regexp for checking the full name

Viewed 23342

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!
6 Answers

Simpler version

    /^([\w]{3,})+\s+([\w\s]{3,})+$/i

([\w]{3,}) the first name should contain only letters and of length 3 or more

+\s the first name should be followed by a space

+([\w\s]{3,})+ the second name should contain only letters of length 3 or more and can be followed by other names or not

/i ignores the case of the letters. Can be uppercase or lowercase letters

/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/g

This version allows accented characters and works fine for me!

^([a-zA-Zà-úÀ-Ú]{2,})+\s+([a-zA-Zà-úÀ-Ú\s]{2,})+$
Related