为jQuery验证插件添加规则 [英] Adding a rule for jQuery validation plugin
本文介绍了为jQuery验证插件添加规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下规则使用jQuery验证方法来验证用户名.我想添加另一个规则,即用户名应仅包含字母数字和下划线字符. 我如何为该规则添加其他方法.如果用户输入的字符少于4个,是否可以打印最小长度错误消息,如果用户输入的字符无效,则是否给出无效字符错误消息?谢谢.
I'm using following rules to validate the username using jQuery validation method. 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? Thanks.
$(document).ready(function() {
$("#sform").validate({
rules: {
username: {
required: true,
minlength: 4
}
},
messages: {
username: "Minimum length 4.",
}
});
});
推荐答案
添加如下内容
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");
并在下面申请
$('validatorElement').validate({
rules : {
username : { alphanumeric : true }
}
});
这篇关于为jQuery验证插件添加规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文