将正则表达式添加到jquery.validate [英] add regex to jquery.validate
问题描述
需要向我的jquery.validate脚本添加一个正则表达式检查.
Need to add a regex check to my jquery.validate script.
我有这个:
$().ready(function() {
$("#myBlahForm").validate({
rules: {
someName: {
required: true,
minlength: 5,
maxlength: 8,
},
somePassword: {
required: true,
minlength: 5,
maxlength: 8,
},
someConfirmPassword: {
required: true,
equalTo: "#somePassword"
},
},
messages: {
someName: {
required: "Please create your username",
},
somePassword: {
required: "Please create a password",
},
someConfirmPassword: {
required: "Please re-enter your password",
equalTo: "Passwords must match"
},
}
});
});
当用户在此处输入数据时,我想检查数据库中允许输入的特定字符:
And I would like to check for specific characters that are allowed in the DB for passwords when the user enters their data here:
<label for="someName">Username</label>
<input type="text" id="someName" name="someName" placeholder="Create a Username"/>
<label for="somePassword">Password</label>
<input type="password" id="somePassword" name="somePassword" placeholder="Create a Password"/>
<label for="someConfirmPassword">Confirm Password </label>
<input type="password" id="someConfirmPassword" name="someConfirmPassword" placeholder="Verify your Password"/>
使用basssistance的jquery.validate插件将正则表达式检查添加到必填字段(例如密码或用户名)的最佳方法是: http://docs.jquery.com/Plugins/Validation
What's the best way to add a regex check to a required field such as a password or username using jquery.validate plugin from bassisstance: http://docs.jquery.com/Plugins/Validation
基本上,我需要确保他们创建的密码和用户名仅包含0-9,字母和几个特殊字符.
Basically, I need to make sure that the password and usernames they create only have 0-9, letters, and a couple of special characters.
推荐答案
好,您可以使用 addMethod
,例如
well, you could add your custom validation method using addMethod
, like
$.validator.addMethod("regx", function(value, element, regexpr) {
return regexpr.test(value);
}, "Please enter a valid pasword.");
然后,
...
$("#myBlahForm").validate({
rules: {
somePassword: {
required: true ,
//change regexp to suit your needs
regx: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])\w{6,}$/,
minlength: 5,
maxlength: 8
}
}
这篇关于将正则表达式添加到jquery.validate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!