How can I validate regex for full name? I only want alphabets (no numericals) and only spaces for the regex. This is what I have done so far. Would you please help me fix th
What Was Asked For
Instead of "^[a-zA-Z][ ]*$"
you want "^[a-zA-Z ]*$"
. There are some answers that reference \s
but you don't want those because they give you other white space like tabs.
Additional Common Examples
On a side note, there are first and last names that contain hypens, like Mary-Ann
for a first name or a hyphenated last name like Jones-Garcia
. There are also last names that have periods, like St. Marc
. Lastly, you have '
in some last names like O'Donnel
.
Side Note
Legally, you could change your name to be Little Bobby Drop Tables... or include other random characters... but I'm not sure how many systems really accommodate for stuff like that.
If you want the general case (world wide), then don't limit the fields by any character type, as you can have names in Greek Letters, Cyrillic Letters, Chinese letters, etc. There are also non English accent characters and other things like the german umlaut.
If you are worried about SQL injection, use parameterized queries instead of dynamic queries.
Suggested Solution
If you are only worried about English letters, and want to use a regular expression that handles the sample cases above, then you could use "^[a-zA-Z \-\.\']*$"