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;
}
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_].
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With