js999 js999 - 1 year ago 96
Javascript Question

regexp for checking the full name

I would like to write a regexp to check if the user inserted at least two words separated by at least one empty space:


var regexp = new RegExp(/^[a-z,',-]+(\s)[a-z,',-]+$/i);

regexp.test("D'avid Camp-Bel"); // true
regexp.test("John ---"); // true // but it should be false!

Answer Source

Does ^[a-z]([-']?[a-z]+)*( [a-z]([-']?[a-z]+)*)+$ work for you?

[a-z] ensures that a name always starts with a letter, then [-']?[a-z]+ allows for a seperating character as long as it's followed by at least another letter. * allows for any number of these parts.

The second half, ( [a-z]([-']?[a-z]+)*) matches a space followed by another name of the same pattern. + makes sure at least one additional name is present, but allows for more. ({1,2} could be used if you want to allow only two or three part names.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download