注释:js文件自行下载
html页面:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery validation之表单验证</title>
<script src="./jquery-1.11.1.min.js"></script>
<script src="./jquery.validate.min.js"></script>
<script>
$(function () {
$("#login").validate({
rules:{//验证规则
name:{
required:true,
minlength:2,
maxlength:10
},
password:{
required:true,
}
},
messages:{//提示信息:与rules是"一一对应"的
name:{
required:'不能为空',
minlength:'最小为2位',
maxlength:'最大为10位'
},
password:{
required:'不能为空',
}
}
})
});
</script>
</head>
<body>
<form action="" method="get" id="login">
用户名:<input name="name" type="text"><br />
密码:<input name="password" type="password" /><br />
<input type="submit" class="button" value="登录">
</form>
</body>
</html>