jQuery Validate
默认校验规则
使用方式
1、将校验规则写到控件中
<script src="../lib/jquery.js"></script>
<script src="../dist/jquery.validate.js"></script>
<script>
$.validator.setDefaults({
submitHandler: function() {
alert("提交事件!");
}
});
$("#commentForm2").validate({
messages: {
email: {
required: 'Enter this!'
}
}
});</script>
<p>
<label for="cemail">E-Mail *</label>
<input id="cemail" name="email" data-rule-required="true" data-rule-email="true" data-msg-email="Please enter a valid email address">
</p>
2、将校验规则写到 js 代码中
$().ready(function() { // 在键盘按下并释放及提交后验证提交表单 $("#signupForm").validate({ rules: { firstname: "required", lastname: "required", username: { required: true, minlength: 2 },
}
messages: {
firstname: "请输入您的名字",
lastname: "请输入您的姓氏",
username: {
required: "请输入用户名",
minlength: "用户名必需由两个字母组成"
},
});
messages 处,如果某个控件没有 message,将调用默认的信息
<p>
<label for="username">用户名</label>
<input id="username" name="username" type="text">
</p>