jQuery验证:如何为正则表达式验证添加规则?
我正在使用jQuery验证插件 。 好东西! 我想迁移现有的ASP.NET解决方案以使用jQuery而不是ASP.NET验证器。 我错过了正则表达式验证器的替代品。 我希望能够做到这样的事情:
$("Textbox").rules("add", { regularExpression: "^[a-zA-Z'.s]{1,40}$" })
如何添加自定义规则来实现此目的?
感谢redsquare的回答,我添加了一个像这样的方法:
$.validator.addMethod(
"regex",
function(value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
现在你需要做的就是验证任何正则表达式是这样的:
$("#Textbox").rules("add", { regex: "^[a-zA-Z'.s]{1,40}$" })
此外,它看起来有一个名为additional-methods.js的文件,其中包含方法“pattern”,它可以是使用不带引号的方法创建的RegExp。
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/additional-methods.js
你可以使用addMethod()
例如
$.validator.addMethod('postalCode', function (value) {
return /^((d{5}-d{4})|(d{5})|([A-Z]d[A-Z]sd[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/
我把所有的东西拼凑在一起做了一个jQuery正则表达式验证器,但我得到了它的工作......这是一个完整的工作示例。 它使用可在jQuery Validation Plugin中找到的'验证'插件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://YOURJQUERYPATH/js/jquery.js" type="text/javascript"></script>
<script src="http://YOURJQUERYPATH/js/jquery.validate.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function() {
$.validator.addMethod("EMAIL", function(value, element) {
return this.optional(element) || /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+.[a-zA-Z.]{2,5}$/i.test(value);
}, "Email Address is invalid: Please enter a valid email address.");
$.validator.addMethod("PASSWORD",function(value,element){
return this.optional(element) || /^(?=.*d)(?=.*[a-z])(?=.*[A-Z]).{8,16}$/i.test(value);
},"Passwords are 8-16 characters with uppercase letters, lowercase letters and at least one number.");
$.validator.addMethod("SUBMIT",function(value,element){
return this.optional(element) || /[^ ]/i.test(value);
},"You did not click the submit button.");
// Validate signup form on keyup and submit
$("#LOGIN").validate({
rules: {
EMAIL: "required EMAIL",
PASSWORD: "required PASSWORD",
SUBMIT: "required SUBMIT",
},
});
});
</script>
</head>
<body>
<div id="LOGIN_FORM" class="form">
<form id="LOGIN" name="LOGIN" method="post" action="/index/secure/authentication?action=login">
<h1>Log In</h1>
<div id="LOGIN_EMAIL">
<label for="EMAIL">Email Address</label>
<input id="EMAIL" name="EMAIL" type="text" value="" tabindex="1" />
</div>
<div id="LOGIN_PASSWORD">
<label for="PASSWORD">Password</label>
<input id="PASSWORD" name="PASSWORD" type="password" value="" tabindex="2" />
</div>
<div id="LOGIN_SUBMIT">
<input id="SUBMIT" name="SUBMIT" type="submit" value="Submit" tabindex="3" />
</div>
</form>
</div>
</body>
</html>
链接地址: http://www.djcxy.com/p/13393.html
上一篇: jQuery validate: How to add a rule for regular expression validation?
下一篇: What is the best regular expression to check if a string is a valid URL?