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
This method validate the name and return false if the name has nothing or has numbers or special characters:
public static boolean isFullname(String str) {
String expression = "^[a-zA-Z\\s]+";
return str.matches(expression);
}
This problem of identifying names is very culture-centric, and it really has no hope of working reliably. I would really recommend against this, as there is NO canonical form for what makes up a person's name in any country, anywhere on Earth that I know of. I could legally change my name to #&*∫Ω∆ Smith
, and that's not going to fit into anyone's algorithm. It's not that this specific example is something that a lot of people do, but many people don't think outside of the ASCII table when considering data input, and that's going to lead to problems.
You can argue against the probability of this happening, but in a global world, it's increasingly unlikely that ALL of your users are going to have English-transliterated spellings for their names. It's also very possible that you'll have users from cultures that don't have a concept of first/last name. Don't assume that, even if your application is only running in a given country, that some of your users won't be from other places (people move from country to country all the time, and some of them might want to use your software).
Protect your app against SQL injection for fields such as this (if you're storing these in a DB), and leave it at that.
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 \-\.\']*$"
[A-Z]
match a single capital letter (first letter of name)
[a-z]*
match any number of small letters (other letters or name)
(\s)
match 1 whitespace character (the space between names)
+
one or more of the previous expression (to match more than one name)
all together:
- matches first names / lastname -
^([A-Z][a-z]*((\s)))+[A-Z][a-z]*$
or to match names like DiMaggio St. Croix, O'Reilly and Le-Pew. You can add similar characters like the 'ᶜ' in MᶜKinley as you remember them or come across people with those less common characters in their name
^([A-z\'\.-ᶜ]*(\s))+[A-z\'\.-ᶜ]*$
Use \s
for spaces. You can also use String.matches(regex) to test if the string matches a regular expression.
public static boolean isFullname(String str) {
String expression = "^[a-zA-Z\\s]*$";
return str.matches(expression);
}
public static boolean isFullname(String str) {
return str.matches("^[a-zA-z ]*$");
}
this method returns true if the string only contains alphabets and spaces.
But it also return true if the string contains nothing.