Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

Example:

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!
like image 989
js999 Avatar asked Jul 17 '12 12:07

js999


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do you verify a first and last name?

Using JavaScript, the full name validation can be easily implemented in the form to check whether the user provides their first name and last name properly. The REGEX (Regular Expression) is the easiest way to validate the Full Name (first name + last name) format in JavaScript.


1 Answers

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.

like image 122
dlras2 Avatar answered Oct 05 '22 00:10

dlras2