andandandand
andandandand

Reputation: 22260

regex for admitting blank spaces between words in a name in jquery validate

I'm using the following rule in jquery validate to make a name field, called nombre, only accept letters.

jQuery.validator.addMethod("nombre", function(value, element, param) {



   return jQuery.trim(value).match(new RegExp("^(?:" + param + ")$"));





});



$("#nombre").rules("add", { nombre: "[a-zA-Z]+"})

I want the regex to accept something like

Polly Jean Harvey

or

P.J. Harvey 

as valid names, (they are currently rejected). How can I add space between words and periods to the regex? Of course, I still want to reject blank names.

Upvotes: 0

Views: 3164

Answers (1)

Colin Hebert
Colin Hebert

Reputation: 93157

You can use [a-zA-Z. ]+ as a regex.

It will accept spaces and dots.

But You can have different names such as "Pierre-Henry de la Brancardière". And this sort of name would be invalid.

As javascript doesn't support unicode with \p I would suggest you to do a blacklist of invalid characters instead of a whitelist.

Upvotes: 1

Related Questions