我正在使用jQuery验证插件。伟大的东西!我想迁移我现有的ASP。NET解决方案使用jQuery代替ASP。净验证器。我缺少正则表达式验证器的替换。我希望能够做到这样:
$("Textbox").rules("add", { regularExpression: "^[a-zA-Z'.\s]{1,40}$" })
我如何添加一个自定义规则来实现这一点?
我正在使用jQuery验证插件。伟大的东西!我想迁移我现有的ASP。NET解决方案使用jQuery代替ASP。净验证器。我缺少正则表达式验证器的替换。我希望能够做到这样:
$("Textbox").rules("add", { regularExpression: "^[a-zA-Z'.\s]{1,40}$" })
我如何添加一个自定义规则来实现这一点?
当前回答
稍微扩展一下PeterTheNiceGuy的回答:
$.validator.addMethod(
"regex",
function(value, element, regexp) {
if (regexp.constructor != RegExp)
regexp = new RegExp(regexp);
else if (regexp.global)
regexp.lastIndex = 0;
return this.optional(element) || regexp.test(value);
},
"Please check your input."
);
这将允许您向规则传递一个regex对象。
$("Textbox").rules("add", { regex: /^[a-zA-Z'.\s]{1,40}$/ });
在RegExp对象上设置g标志时,必须重置lastIndex属性。否则,它将从与该正则表达式的最后一个匹配的位置开始验证,即使主题字符串不同。
我的其他一些想法是允许你使用正则表达式的数组,还有另一个正则表达式的否定规则:
$("password").rules("add", {
regex: [
/^[a-zA-Z'.\s]{8,40}$/,
/^.*[a-z].*$/,
/^.*[A-Z].*$/,
/^.*[0-9].*$/
],
'!regex': /password|123/
});
但实施这些可能会太过了。
其他回答
你可以使用addMethod()
e.g
$.validator.addMethod('postalCode', function (value) {
return /^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/.test(value);
}, 'Please enter a valid US or Canadian postal code.');
这里有好文章https://web.archive.org/web/20130609222116/http://www.randallmorey.com/blog/2008/mar/16/extending-jquery-form-validation-plugin/
没有理由将正则表达式定义为字符串。
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var check = false;
return this.optional(element) || regexp.test(value);
},
"Please check your input."
);
and
telephone: { required: true, regex : /^[\d\s]+$/, minlength: 5 },
这样更好,不是吗?
这是工作代码。
function validateSignup()
{
$.validator.addMethod(
"regex",
function(value, element, regexp)
{
if (regexp.constructor != RegExp)
regexp = new RegExp(regexp);
else if (regexp.global)
regexp.lastIndex = 0;
return this.optional(element) || regexp.test(value);
},
"Please check your input."
);
$('#signupForm').validate(
{
onkeyup : false,
errorClass: "req_mess",
ignore: ":hidden",
validClass: "signup_valid_class",
errorClass: "signup_error_class",
rules:
{
email:
{
required: true,
email: true,
regex: /^[A-Za-z0-9_]+\@[A-Za-z0-9_]+\.[A-Za-z0-9_]+/,
},
userId:
{
required: true,
minlength: 6,
maxlength: 15,
regex: /^[A-Za-z0-9_]{6,15}$/,
},
phoneNum:
{
required: true,
regex: /^[+-]{1}[0-9]{1,3}\-[0-9]{10}$/,
},
},
messages:
{
email:
{
required: 'You must enter a email',
regex: 'Please enter a valid email without spacial chars, ie, Example@gmail.com'
},
userId:
{
required: 'Alphanumeric, _, min:6, max:15',
regex: "Please enter any alphaNumeric char of length between 6-15, ie, sbp_arun_2016"
},
phoneNum:
{
required: "Please enter your phone number",
regex: "e.g. +91-1234567890"
},
},
submitHandler: function (form)
{
return true;
}
});
}
稍微扩展一下PeterTheNiceGuy的回答:
$.validator.addMethod(
"regex",
function(value, element, regexp) {
if (regexp.constructor != RegExp)
regexp = new RegExp(regexp);
else if (regexp.global)
regexp.lastIndex = 0;
return this.optional(element) || regexp.test(value);
},
"Please check your input."
);
这将允许您向规则传递一个regex对象。
$("Textbox").rules("add", { regex: /^[a-zA-Z'.\s]{1,40}$/ });
在RegExp对象上设置g标志时,必须重置lastIndex属性。否则,它将从与该正则表达式的最后一个匹配的位置开始验证,即使主题字符串不同。
我的其他一些想法是允许你使用正则表达式的数组,还有另一个正则表达式的否定规则:
$("password").rules("add", {
regex: [
/^[a-zA-Z'.\s]{8,40}$/,
/^.*[a-z].*$/,
/^.*[A-Z].*$/,
/^.*[0-9].*$/
],
'!regex': /password|123/
});
但实施这些可能会太过了。
你可以使用extra -methods.js文件中定义的模式。注意这个additional-methods.js文件必须包含在jQuery验证依赖之后,然后你就可以使用它了
$("#frm").validate({ rules: { Textbox: { pattern: /^[a-zA-Z'.\s]{1,40}$/ }, }, messages: { Textbox: { pattern: 'The Textbox string format is invalid' } } }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/jquery.validate.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.17.0/additional-methods.min.js"></script> <form id="frm" method="get" action=""> <fieldset> <p> <label for="fullname">Textbox</label> <input id="Textbox" name="Textbox" type="text"> </p> </fieldset> </form>