Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Regex for Full Name

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;
}
like image 419
Nicholas Lie Avatar asked Sep 09 '11 13:09

Nicholas Lie


People also ask

How do you write names in regex?

String regularExpression= "^[A-Za-z][A-Za-z0-9_]{7,29}$"; A valid username should start with an alphabet so, [A-Za-z]. All other characters can be alphabets, numbers or an underscore so, [A-Za-z0-9_].

How do you verify a first and last name?

Using JavaScript, the full name validation can be easily implemented in the form to check whether the user provides their first name and last name properly. The REGEX (Regular Expression) is the easiest way to validate the Full Name (first name + last name) format in JavaScript.


1 Answers

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);        
}
like image 134
Ahmed El Reweny Avatar answered Sep 23 '22 10:09

Ahmed El Reweny