<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery validation plug-in - main demo</title>
<link rel="stylesheet" type="text/css" media="screen" href="css/screen.css" />
<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="jquery.validate.js"></script>
<script type="text/javascript">
/*自定义函数
$.validator.addMethod("checkCartLength",function(value,element,params){})
$.validator.addMethod固定写法
checkCartLength:自定义的函数名称
function(value,element,params):函数
value:要验证的文本框输入的值
element:文本框这个对象
params:传递的参数值
*/
//验证身价证的长度
$.validator.addMethod("checkCardLength",function(value,element,params){
var len = value.length;
if(len!=15&&len!=18) {
return false;
}
return true;
});
//验证15位身价证的
$.validator.addMethod("checkCardLength15",function(value,element,params){
var len = value.length;
if(len==15) {
var pattern = /^[\d]{15}$/;
var flag = pattern.test(value);
if(!flag) {
return false;
}
return true;
}
return true;
});
//验证18位身价证的
$.validator.addMethod("checkCardLength18",function(value,element,params){
var len = value.length;
if(len==18) {
var pattern = /^[\d]{18}|[\d]{17}[X]{1}$/;
var flag = pattern.test(value);
if(!flag) {
return false;
}
return true;
}
return true;
});
$(document).ready(function() {
$("#empForm").validate({
rules: {
realname: {
required:true
},
username: {
required:true,
rangelength:[5,8]
},
psw: {
required:true,
rangelength:[6,12]
},
psw2: {
required:true,
rangelength:[6,12],
equalTo:"#psw"
},
gender: {
required:true
},
age: {
required:true,
range:[26,50]
},
edu: {
required:true
},
birthday: {
required:true,
date:true
},
checkbox1: {
required:true
},
email: {
required:true,
email:true
},
card: {
required:true,
checkCardLength:1,
checkCardLength15:1,
checkCardLength18:1
}
},
messages: {
realname: {
required:"真实姓名不能为空"
},
username: {
required:"登录名不能为空",
rangelength:"登录名长度应该在5-8之间"
},
psw: {
required:"密码不能为空",
rangelength:"密码长度应该在6-12之间"
},
psw2: {
required:"确认密码不能为空",
rangelength:"确认密码长度应该在6-12之间",
equalTo:"两次输入的密码不一致"
},
gender: {
required:"您没有第三性别可选"
},
age: {
required:"年龄不能为空",
range:"年龄应该在26-50之间"
},
edu: {
required:"必须选择一个学历"
},
birthday: {
required:"出生日期不能为空",
date:"日期格式不对"
},
checkbox1: {
required:"至少选择一个爱好"
},
email: {
required:"电子邮件不能为空",
email:"必须输入正确的电子邮件"
},
card: {
required:"身价证不能为空",
checkCardLength:"身价证长度输入有误",
checkCardLength15:"您的15位身价证输入有误",
checkCardLength18:"您的18位身价证输入有误"
}
}
});
});
</script>
</head>
<body>
<p>员工信息录入</p>
<form name="empForm" id="empForm" method="post" action="test.html">
<table border=1>
<tr>
<td>真实姓名(不能为空 ,没有其他要求)</td>
<td><input type="text" id="realname" name="realname" />
</td>
</tr>
<tr>
<td>登录名(登录名不能为空,长度应该在5-8之间,可以包含中文字符(一个汉字算一个字符)):</td>
<td><input type="text" id="username" name="username" /></td>
</tr>
<tr>
<td>密码(不能为空,长度6-12字符或数字,不能包含中文字符):</td>
<td><input type="password" id="psw" name="psw" style="width:120px" /></td>
</tr>
<tr>
<td>重复密码密码(不能为空,长度6-12字符或数字,不能包含中文字符):</td>
<td><input type="password" id="psw2" name="psw2" style="width:120px" /></td>
</tr>
<tr>
<td>性别(必选其一)</td>
<td>
<input type="radio" id="gender_male" value="m" name="gender"/>男
<input type="radio" id="gender_female" value="f" name="gender"/>女
<label style="display:none" for="gender" class="error">请选择性别</label>
</td>
</tr>
<tr>
<td>年龄(必填26-50):</td>
<td><input type="text" id="age" name="age" /></td>
</tr>
<tr>
<td>你的学历:</td>
<td> <select name="edu" id="edu">
<option value="">--请选择你的学历--</option>
<option value="a">专科</option>
<option value="b">本科</option>
<option value="c">研究生</option>
<option value="e">硕士</option>
<option value="d">博士</option>
</select>
</td>
</tr>
<tr>
<td>出生日期(1982/09/21):</td>
<td><input type="text" id="birthday" name="birthday" style="width:120px" value="" /></td>
</tr>
<tr>
<td>兴趣爱好:</td>
<td colspan="2">
<input type="checkbox" name="checkbox1" id="qq1"/>乒乓球
<input type="checkbox" name="checkbox1" id="qq2" value="1" />羽毛球
<input type="checkbox" name="checkbox1" id="qq3" value="2" />上网
<input type="checkbox" name="checkbox1" id="qq4" value="3" />旅游
<input type="checkbox" name="checkbox1" id="qq5" value="4" />购物
<label style="display: none" for="checkbox1" class="error">您的兴趣爱好,至少选择一个</label>
</td>
</tr>
<tr>
<td align="left">电子邮箱:</td>
<td><input type="text" id="email" style="width:120px" name="email" /></td>
</tr>
<tr>
<td align="left">身份证(15-18):</td>
<td><input type="text" id="card" style="width:200px" name="card" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="firstname" id="firstname" value="保存"></td>
</tr>
</table>
</form>
</body>
</html>
jquery validate检验表单
最新推荐文章于 2024-09-07 13:38:28 发布