Skip to content
Advertisement

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?

Advertisement

Answer

Instead of the below regex:

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

Use this:

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

This will also take the space.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement