Reputation: 5525
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.
Upvotes: 9
Views: 39957
Reputation: 34107
working demo http://jsfiddle.net/cAADx/
/^[a-z0-9\-\s]+$/i
should do the trick!
g = /g modifier makes sure that all occurrences of "replacement"
i = /i makes the regex match case insensitive.
good read: http://www.regular-expressions.info/javascript.html
Hope this helps,
code
$(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"
}
}
});
});
Will remove this image in 2 mins see here robert like this http://jsfiddle.net/5ykup/
Upvotes: 27
Reputation: 757
I think it will work if you pass the following RegExp as param
:
[A-za-z0-9_\-\s]+
Upvotes: 2