2

I seem not to get it right to validate a user FirstName + LastName and checking for MiddleName like this:

John Doe - Valid
John M Doe - Valid
John Mr Doe - Invalid
John m Doe - Invalid

So, MiddleName can only be an uppercase letter.

What I came up with:

  • FirstName + LastName should have at least 3 chars - OK
  • Should be always FirstName and a LastName - OK

Cannot figure out how to check for MiddleName to have only 1 char if is present.

My current regex is like this:

var isValid = /^((\b[a-zA-Z]{3,40}\b)\s*){2,}$/.test($(this).val());
// ^((\b[a-zA-Z]{3,40}\b)\s*){2,}$ - actual regex
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
123onetwothree
  • 546
  • 1
  • 7
  • 16
  • Can you please clarify if the max chars First and Last Name can have is 40? Is there any max char limit for them? I mean, if there can be any number of chars in First and Last names, why use `40` in the limiting quantifiers? – Wiktor Stribiżew Oct 13 '17 at 09:31

2 Answers2

4

You may use

/^[a-zA-Z]{3,40}(?:\s[A-Z])?\s[a-zA-Z]{3,40}$/

See the regex demo. NOTE: {3,40} is a limiting quantifier that matches 3 to 40 consecutive occurrences of the quantified subpattern (that is, [a-zA-Z] letter pattern here. If you do not intend to limit the upper bound (the max letter limit) for the First and Last Names, just remove 40.

Details

  • ^ - start of string
  • [a-zA-Z]{3,40} - 3 to 40 ASCII letters
  • (?:\s[A-Z])? - (here) an optional sequence of
    • \s - a whitespace
    • [A-Z] - a single uppercase ASCII letter
  • \s - a whitespace
  • [a-zA-Z]{3,40} - 3 to 40 ASCII letters
  • $ - end of string.

JS demo:

var strs = [' John Doe', 'John M Doe', 'John Mr Doe', 'John m Doe'];
var rx = /^[a-zA-Z]{3,40}(?:\s[A-Z])?\s[a-zA-Z]{3,40}$/;
for (var s of strs) {
  console.log(s, "=>", rx.test(s));
}
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • Can i enforce that char (middlename) to be capital letter ? – 123onetwothree Oct 13 '17 at 09:20
  • @Wiktor You can use `{3,}` for 3 or more characters, without limiting it to 40. – chocochaos Oct 13 '17 at 09:23
  • That is taken from the original regex. If there is no upper bound, then sure, `{3,}` should be used to match 3 *or more* consecutive occurrences of the quantified subpattern. OP frequently do not write in words the exact requirements, but their attempts speak more than the words. – Wiktor Stribiżew Oct 13 '17 at 09:26
  • @123onetwothree I updated your question to include this last-minute requirement. – Wiktor Stribiżew Oct 13 '17 at 09:30
  • is the non-capture pattern (?:) necessary here or is it just a good practice to add it – Thirumalai Parthasarathi Oct 13 '17 at 09:52
  • 1
    @ThirumalaiParthasarathi A [non-capturing group](https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-what-does-a-question-mark-followed-by-a-colon) in such a small pattern is not giving any significant boost, it is just a best practice to use capturing groups only when you need to access the captured substrings later. Here, it is validation OP cares about, so there is no point *capturing* any part of the match. Some would use a capturing group here for readability reasons, however, I do not see any problem with the `(?:...)` readability. – Wiktor Stribiżew Oct 13 '17 at 09:55
2

Try this regex:

/^[A-Za-z]{3,}\s+([A-Z]\s+)?[A-Za-z]{3,}$/

Check here for a demo.


Explanation of how it works:

  • ^ start of string.
  • [A-Za-z]{3,} checks for 3 or more letters.
  • \s+ checks for one or more whitespace characters.
  • ([A-Z]\s+)? checks for one capital letter character followed by one or more whitepscae characters, optionally. So it either matches a single letter + spaces, or nothing.
  • [A-Za-z]{3,} checks for 3 or more letters, again.
  • $ end of string.
chocochaos
  • 1,355
  • 9
  • 15