I have this code:
$(document).ready(function(){
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Only alphabetical characters");
But if I insert a double name like "Mary Jane" the space creates a problem. how can i allow spaces too in my rule?
You need to add the whitespace character (\s
) to your Regex:
jQuery.validator.addMethod("lettersonly", function(value, element) {
return this.optional(element) || /^[a-z\s]+$/i.test(value);
}, "Only alphabetical characters");