Jquery Validation: allow only alphabets and spaces
jQuery.validator.addMethod("lettersonlys", function(value, element) {
return this.optional(element) || /^[a-zA-Z ]*$/.test(value);
}, "Letters only please");
Use pattern: /^[a-zA-Z ]*$/
Your validation is proper. You just need to change regex /^[a-zA-Z ]*$/
$.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z ]*$/);
});
Just leave a space or use \s
in your regex:
$.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z\s]+$/);
// -- or leave a space here ^^
});
Instead of the below regex:
/^[a-zA-Z]+$/
Use this:
/^[a-zA-Z\s]+$/
This will also take the space.