日期:2014-05-16  浏览次数:20460 次

js检测小例子
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function getdate(){
var date=new Date();
var year=date.getFullYear();
var month=date.getMonth()+1;
var day=date.getDay();
alert(year+"-"+month+"-"+day);
}
//全选
function selectAll(){
var form1=document.forms['Form1'];
for(var i=0;i<form1.length;i++ ){
  var e = form1.elements[i];
  if (e.type == "checkbox")
   e.checked = !e.checked;
}
}
//增加options
function addoption(){
var slecet=document.getElementById("select1");
slecet.options.add(new Option('fruit','4'));
}
//删除options
function deloption(){
var slecet=document.getElementById("select1").remove(0);
//slecet.options.remove(0);

}
//
function check (str) {
  isEmail1=/^\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w+$/;
  isEmail2=/^.*@[^_]*$/;
  return (isEmail1.test(str)&&isEmail2.test(str));
}
//检测邮箱地址
function checkEmail(str){
if(!check(str)){
alert("邮箱地址错误");
}
}
function IsDigit(cCheck) { return (('0'<=cCheck) && (cCheck<='9')); }
function IsAlpha(cCheck) { return ((('a'<=cCheck) && (cCheck<='z')) || (('A'<=cCheck) && (cCheck<='Z'))) }
//检测字符串(假设长度18,前15为字母,后三为数字)
function checkIdnumber(str){
var str1=str.substring(0,16);

if(!IsAlpha(str1)){
alert("前15必须为字母");
}


var str2=str.substring(16,str.length)


if(!IsDigit(str2)){
alert("后三位必为数字");

}

}

</script>
</head>
<body>
<select name="select1" id="select1">
<option value="1">apple</option>
<option value="2">orange</option>
<option value="3">banana</option>
</select>
<form action="" name="Form1" method="post" onsubmit="">

<p><input type="checkbox" name="test" value="1"></p>
<p><input type="checkbox" name="test" value="2"></p>
<p><input type="checkbox" name="test" value="3"></p>

<div>
<p><input type="submit" name="true" value="提交"></p>
</div>

</form>
<div>
<p><input type="button" value="全选" name="true" onclick="selectAll()">
<input type="button" value="add" name="true" onclick="addoption()">
<input type="button" value="del" name="true" onclick="deloption()">
<input type="button" value="date" name="true" onclick="getdate()">
</p>
<p><input type="text" value="" name="email"> <input
type="button" value="checkemail" name="true"
onclick="checkEmail(document.getElementById('email').value)"></p>
<p><input type="text" value="" name="idnumber"> <input
type="button" value="checknumber" name="true"
onclick="checkIdnumber(document.getElementById('idnumber').value)"></p>

</div>

</body>
</html>