jQuery Validator:验证AlphaNumeric +空格和破折号 [英] jQuery Validator : Validate AlphaNumeric + Space and Dash
问题描述
我的网站上安装了jQuery验证插件(http://docs.jquery.com/Plugins/Validation).
I have jQuery validation plugins (http://docs.jquery.com/Plugins/Validation) installed on my website.
我正在使用此代码来验证文本字段中的字母数字,并且可以正常工作.但这不允许空格和破折号(-).
I'm using this code to validate alpha-numeric from text field, and it works. but it doesn't allow space and dash (-).
$.validator.addMethod("titleAlphaNum", function(value, element, param)
{
return value.match(new RegExp("^" + param + "$"));
});
如何使其与空格和破折号一起使用?谢谢.
how to make it works with space and dash? thanks.
推荐答案
工作演示 http: //jsfiddle.net/cAADx/
/^[a-z0-9\-\s]+$/i
应该可以解决问题!
g =/g修饰符可确保所有出现的替换"
g = /g modifier makes sure that all occurrences of "replacement"
i =/i使正则表达式匹配不区分大小写.
i = /i makes the regex match case insensitive.
好读: http://www.regular-expressions. info/javascript.html
希望这会有所帮助,
代码
$(function() {
$.validator.addMethod("loginRegex", function(value, element) {
return this.optional(element) || /^[a-z0-9\-\s]+$/i.test(value);
}, "Username must contain only letters, numbers, or dashes.");
$("#myForm").validate({
rules: {
"login": {
required: true,
loginRegex: true,
}
},
messages: {
"login": {
required: "You must enter a login name",
loginRegex: "Login format not valid"
}
}
});
});
将在2分钟内删除此图片,在此处查看罗伯特这样的 http://jsfiddle. net/5ykup/
Will remove this image in 2 mins see here robert like this http://jsfiddle.net/5ykup/
这篇关于jQuery Validator:验证AlphaNumeric +空格和破折号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!