Reputation: 1
I am validating a text field to allow only letters and spaces.
This is the method I am using:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Please enter only letters");
But the regular expression is not allowing spaces between words.
I tried :
/^[a-z ]+$/i.test(value)
and /^[a-z\s]+$/i.test(value)
jQuery version - 1.7.1
Upvotes: 0
Views: 15547
Reputation: 553
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-zA-Z\s]*$/.test(value);
},"Please enter only letters");
replace this : /^[a-z\s]+$/i
by this :/^[a-zA-Z\s]*$/
I checked It works.
Upvotes: 0