I am trying to get this Regex to work to validate a Name field to only allow A-Z, \' and -.
So far I am using this which is working fine apart from it wont allow an
Your code already does what you want it to:
... prints:
Valid: Jim
Valid: John
Valid: O'Toole
Valid: one-two
Valid: Daniel'Blackmore
Invalid: Jim
Invalid: abc123
Invalid: $@#$%@#$%&*(*&){}//;;
The single quote does not have any special meaning in regular expressions so it needs no special treatment. The minus sign (-
), when inside []
, means range; if you need a literal -
it has to be the first or last character, as in your code.
Said that, the error (if any) is somewhere else.