1

I am trying to validate a name, with the following rules:

  1. Case insensitive

  2. Only letters, blank space, accented characters.

  3. At least one blank space followed with some letter or letters John D or john Doe or John X Doe

  4. Cannot have multiple spaces at once John X Doe

  5. Length must be at least 5 (with space) and at maximum 80

I came up with (?i)^[a-z]+(?:[\ ]?[a-z]+)*$

https://regex101.com/r/bQ5oO9/1

Some problems:

  1. Is validating only 'Name'

  2. Is not validating the length

  3. Is not accepting accented characters

Julian
  • 33,915
  • 22
  • 119
  • 174
2Fast4YouBR
  • 1,012
  • 1
  • 12
  • 22

1 Answers1

1

You may use

^(?=.{5,80}$)\p{L}*(?: \p{L}+)+$

See this regex demo (I recommend \z rather than $ since \z matches the vary end of the string, however, if you use a plain space in the pattern, that is irrelevant).

Details:

  • ^ - start of string
  • (?=.{5,80}$) - the length must be at least 5 and at most 80 chars all in all
  • \p{L}* - zero or more letters (use + instead of * if the string cannot start with a space)
  • (?: \p{L}+)+ - 1 or more sequences of one space and one or more letters (this meets your Cannot have multiple spaces at once and At least one blank space followed with some letter or letters requirements)
  • $ - end of string.

Note that \p{L} matches any Unicode base letters, thus meeting your Only letters, blank space, accented characters and case insensitive requirements.

If you need to also support diacritic symbols, replace \p{L} with [\p{L}\p{M}].

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563