Hope someone can help me with a regular expression to validate names. The criteria are that the string must be between 1 and 30 characters in length and will allow the following: uppercase alpha, lowercase alpha, space, apostrophe, full stop (or period) and hyphen.
I’ve got a regex that will do this, but the complication is that the "special" characters (space, apostrophe, full stop, hyphen) may not be consecutive. So you could not have this: "Smithers-'Jones" (hyphen followed by apostrophe), or this: "Smithers –Jones" (space followed by hyphen), or this "O''Reilly" (consecutive apostrophes).
From what I’ve read so far, I think I need to use back-references in some way, but I haven't managed to get anything working yet. I've seen examples that detect repeating characters, but this is not quite the same.
Any help would be appreciated.
Consecutive means one after another. And identical means the exact same. So this means that. There are 3 exact same characters that repeated. “AAA”
Match any specific character in a setUse square brackets [] to match any characters in a set. Use \w to match any single alphanumeric character: 0-9 , a-z , A-Z , and _ (underscore). Use \d to match any single digit. Use \s to match any single whitespace character.
This regex should work:
^(?!.*?[ '.-]{2})[A-Za-z0-9 '.-]{1,30}$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With