Working on a name-surname validator. We get entries by people with more than one name. I am looking for a solution.
Old regex: /[a-zA-Z]{2,}\s[a-zA-Z]{2,}$/ (Works for 1 name – 1 surname)
I am confused with part {2,} that it counts recurrence of pattern in the word, but I want it to count the words -> {2,4} for minimum 2 and maximum 4 words that fits the rule.
This doesn’t work: /[a-zA-Z\s]{2,}\w$/
>Solution :
Matching 2-4 words, where a "word" matches 2 or more chars A-Za-z:
^[a-zA-Z]{2,}(?:\s[a-zA-Z]{2,}){1,3}$
Note that the current character ranges are pretty strict for names.
You might also write it matching 1+ word characters, but it would still be limited:
^\w+(?:\s+\w+){1,3}$