[英]Adding a rule for jQuery validation plugin
I'm using following rules to validate the username using jQuery validation method. 我正在使用以下规则来使用jQuery验证方法验证用户名。 I want to add another rule that username should contain only alphanumeric and underscore character. 我想添加另一个规则,即用户名应该只包含字母数字和下划线字符。 How can i add additional method for that rule. 如何为该规则添加其他方法。 Is it possible that if user gives less than 4 characters, then I print the minimum length error message, and if the user gives invalid characters, then I give the invalid character error message? 如果用户提供少于4个字符,那么我可能会打印最小长度错误消息,如果用户提供无效字符,那么我会给出无效字符错误消息吗? Thanks. 谢谢。
$(document).ready(function() {
$("#sform").validate({
rules: {
username: {
required: true,
minlength: 4
}
},
messages: {
username: "Minimum length 4.",
}
});
});
add like below 添加如下
jQuery.validator.addMethod("alphanumeric", function(value, element) {
return !jQuery.validator.methods.required(value, element) || /^[a-zA-Z0-9_]+$/i.test(value);
}
, "Letters, numbers or underscores only please");
and apply below 并在下面申请
$('validatorElement').validate({
rules : {
username : { alphanumeric : true }
}
});
The validation plugin comes with additional-methods.js, which includes an alphanumeric plus underscore validation: 验证插件附带了additional-methods.js,其中包括字母数字和下划线验证:
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js
Same rules as above: 与上述规则相同:
$('validatorElement').validate({
rules: {
username: { alphanumeric : true }
}
});
Using remote ajax validation do this 使用远程ajax验证执行此操作
$("#sform").validate({
rules: {
username: {
required: true,
minlength: 4,
remote: 'alphanumertic.php'
//check on server
}
},
messages: {
username: "Minimum length 4.",
}
});
Probably the best way is to validate using regular expression which can be found on 可能最好的方法是使用可在其上找到的正则表达式进行验证
jQuery validate: How to add a rule for regular expression validation? jQuery验证:如何为正则表达式验证添加规则?
Regular Expression for alphanumeric and underscores 字母数字和下划线的正则表达式
Old question but I am adding my answer so that one can get help 老问题,但我正在添加我的答案,以便人们可以得到帮助
One can use following: 可以使用以下内容:
create a function that will encapsulate our validation test, with the following signatures: function mytest(value, element, params){...}
创建一个封装我们的验证测试的函数,具有以下签名: function mytest(value, element, params){...}
After we have the function established, we can then attach it to the jQuery Validation plug-in. 在我们建立了函数之后,我们可以将它附加到jQuery Validation插件。 To do this, we call the validator object's addMethod() function. 为此,我们调用验证器对象的addMethod()函数。
My Code: 我的代码:
$(document).ready(function() {
// other code
// :
// :
$.validator.addMethod(
"passwd",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Invalid input"
);
$("#add_user_form").validate({
rules:{
user_name: {
required: true,
minlength: 5,
maxlength: 15,
noSpace: true
},
password: {
required: true,
minlength: 8,
passwd: "((?=(.*\\d.*){2,})(?=(.*[a-zA-Z].*){2,})(?=(.*[@#$(){}!~,.!^?/|+=-_%].*){2,}).{8,20})"
}
},
messages:{
user_name: {
required: "*",
minlength: " at least 5 characters",
maxlenght: " only 15 characters",
noSpace: " No space Please"
},
password: {
required: "*",
minlength: " Your password must be at least 8 characters long",
passwd: " Invalid Password choice"
}
});
//
// more code in document.ready
good links: 良好的链接:
Starting With jQuery - How to Write Custom Validation Rules 从jQuery开始 - 如何编写自定义验证规则
How to add a rule for regular expression validation? 如何为正则表达式验证添加规则?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.