Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Validation: allow only alphabets and spaces

My validation accepts only alphabets. I want allow spaces as well.

$.validator.addMethod("alpha", function(value, element) {
    return this.optional(element) || value == value.match(/^[a-zA-Z]+$/);
});

What change needs to be done here?

like image 670
monda Avatar asked Apr 01 '14 05:04

monda


2 Answers

Instead of the below regex:

/^[a-zA-Z]+$/

Use this:

/^[a-zA-Z\s]+$/

This will also take the space.

like image 90
Code Lღver Avatar answered Nov 15 '22 18:11

Code Lღver


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 ^^
});
like image 3
Felix Avatar answered Nov 15 '22 17:11

Felix