10

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 the regex? Thank you very much

public static boolean isFullname(String str) {
    boolean isValid = false;
    String expression = "^[a-zA-Z][ ]*$"; //I know this one is wrong for sure >,<
    CharSequence inputStr = str;
    Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(inputStr);
    if (matcher.matches()) {
        isValid = true;
    }
    return isValid;
}
5
  • 4
    Don’t make any assumptions about people’s names. Period. Don’t use [a-z] when you should be using [\pL\pM\p{Nl}]. Don’t forget punctuation. Commented Sep 9, 2011 at 14:00
  • 4
    How about people who have names like Scarlett O'Hara or Bashar al-Assad? Commented Sep 9, 2011 at 14:03
  • 2
    To quote tchrist, "Code that believes someone’s name can only contain certain characters is stupid, offensive, and wrong." Commented Sep 9, 2011 at 14:14
  • 1
    Thank you very much tchrist. I now realize that I shouldn't be validating regexes for full names. But still I would have to think that even symbols can pass the validation as well "%^@^#!@*" Commented Sep 9, 2011 at 14:26
  • stackoverflow.com/questions/2385701/… Commented Mar 31, 2013 at 16:18

6 Answers 6

23

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.

Sign up to request clarification or add additional context in comments.

1 Comment

... I could legally change my name to #&*∫Ω∆ Smith, and that's not going to fit into anyone's algorithm... how about ^.*$ :)
7

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);        
}

Comments

3

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 \-\.\']*$"

Comments

0

[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\'\.-ᶜ]*$

10 Comments

Joe DiMaggio may have something to say about that :-)
@NullUser, that supports middle names.
@pax, Mr DiMaggio may well have a problem. I'll revise
Fails on “John Paul Jones”. Fails on “Renée Fleming”. Fails on “Dominque Strauss‐Kahn”. Fails on “King Henry Ⅷ”. Fails on “Tim O’Reilly”. Fails on “Secretary Federico Peña”. Fails on “Motel 6”. Fails on “Cher”. Fails on “Antonio Cipriano José María y Francisco de Santa Ana Machado y Ruiz”. Fails fails fails fails fails fails fails fails fails.
False positive on “___^^^_^\\\\[[[]][][____”.
|
-3
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.

Comments

-3

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);        
}

4 Comments

False positive on the empty string. False negative on “Dominque Strauss‐Kahn”.
@tchrist Why do you assume everyone has a name?
There's no need to use ^ and $ with .matches()
False positive on “\n\f\f\t”.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.