Dev*_*per 15 validation jquery jquery-validate
如何在div容器中验证具有三个规则和三个自定义消息的电子邮件地址字段.即.
rules: {
email: {
validationRule: true,
email: true,
remote: '/ajax/emailDuplicationCheck.php'
}
}
Run Code Online (Sandbox Code Playgroud)
如果第一条错误信息应为"验证规则失败"
如果第二个错误(失败)"输入电子邮件地址"
如果第三(远程)失败.消息应为"帐户已存在于数据库中".
我可以为所有规则添加一条消息,但我想根据规则自定义消息.
Val*_*sel 13
试试这个:
$("#myForm").validate({ // Replace #myForm with the ID of your form
rules: {
email: {
required: true,
email: true,
remote: {
url: "/ajax/emailDuplicationCheck.php",
type: "post",
data: { email: function() {
return $("#email").val(); // Add #email ID to your email field
}
}
}
},
messages: {
email: {
required: 'Email address is required',
email: 'Please enter a valid email address',
remote: 'This email address has already been used'
}
},
errorPlacement: function(error) {
$("#response").html(error);
}
});
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助 !
您可以为每个规则使用自定义验证规则以及您自己的自定义消息。
为简单起见,下面是如何使用三种自定义验证方法(每种方法都有其自己的错误消息)来验证“用户名”输入的示例:
// Add three custom methods first:
$.validator.addMethod("nameCustom", function(value, element) {
return this.optional(element) || /^[a-zA-Z ]+/.test(value);
}, 'Please use english letters only.');
$.validator.addMethod("noSpaceStart", function(value, element) {
return value.indexOf(" ") != 0;
}, "Name cannot start with a space");
$.validator.addMethod("noSpaceEnd", function(value, element) {
return value.lastIndexOf(" ") != value.length - 1;
}, "Name cannot end with a space");
$('#form_to_validate').validate({
rules: {
username: {
// Add the custom validation methods to the username input
noSpaceStart: true,
noSpaceEnd: true,
nameCustom: true,
// required and maxlength are built-in methods in the plugin and are ready to be used
required: true,
maxlength: 50
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18710 次 |
| 最近记录: |