<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Validate</title>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<form action="add" id="signupForm">
<input type="text" name="test" data-rule-required="true" data-msg-maxlength="必须输入">
<input type="text" name="username" data-rule-af="a,f" data-msg-af="必须是a-f">
<button type="submit" class="btn green"></i> 提交</button>
</form>
<script>
// 自定义方法
$().ready(function() {
$("#signupForm").validate({
submitHandler:function(form){
// alert("submitted");
form.submit();
},
debug:true,
});
});
$.validator.addMethod("af",function(value,element,params){
if(value.length>1){
return false;
}
if(value>=params[0] && value<=params[1]){
return true;
}else{
return false;
}
},"必须是一个字母,且a-f");
</script>
</body>
</html>