user1313360
user1313360

Reputation: 1

Allow only letters and spaces - jquery validation plugin

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

Answers (2)

kishor10d
kishor10d

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

gdoron
gdoron

Reputation: 150313

The regular expression is:

^[a-z\s]*$

LIVE DEMO

Upvotes: 9

Related Questions