Java Regex to Validate Full Name allow only Spaces and Letters

Java Regex to Validate Full Name allow only Spaces and Letters

To validate a full name using a regular expression in Java and allow only spaces and letters, you can use the following pattern:

import java.util.regex.Matcher; import java.util.regex.Pattern; public class ValidateFullName { public static void main(String[] args) { String fullName = "John Doe"; boolean isValid = isValidFullName(fullName); System.out.println("Is the full name valid? " + isValid); } public static boolean isValidFullName(String fullName) { // Allow only spaces and letters in the full name String regex = "^[a-zA-Z\\s]+$"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(fullName); return matcher.matches(); } } 

In this example:

  • ^[a-zA-Z\\s]+$ is the regular expression.
    • ^ asserts the start of the string.
    • [a-zA-Z\\s] allows any letter (both uppercase and lowercase) or space.
    • + allows one or more occurrences of the preceding pattern.
    • $ asserts the end of the string.

The isValidFullName method returns true if the full name contains only spaces and letters; otherwise, it returns false.

Adjust the input fullName as needed. If the input full name is valid (contains only spaces and letters), the output will be:

Is the full name valid? true 

Modify the regex or adjust the code based on your specific requirements.

Examples

  1. Java Regex to Allow Only Alphabets and Spaces in Full Name:

    String fullName = "John Doe"; String regex = "^[a-zA-Z\\s]+$"; 
  2. Validate Full Name with Java Regex Allowing Middle Name:

    String fullName = "John Michael Doe"; String regex = "^[a-zA-Z]+( [a-zA-Z]+)*$"; 
  3. Java Regex to Validate Full Name with Apostrophes:

    String fullName = "Mary O'Connor"; String regex = "^[a-zA-Z'\\s]+$"; 
  4. Allow Only Letters and Spaces in Java Regex for Full Name:

    String fullName = "Jane Smith"; String regex = "^[a-zA-Z\\s]+$"; 
  5. Validate Full Name with Java Regex Allowing Hyphens:

    String fullName = "Anne-Marie Johnson"; String regex = "^[a-zA-Z\\s-]+$"; 
  6. Java Regex to Allow Only Alphabets, Spaces, and Dots in Full Name:

    String fullName = "Dr. James Brown"; String regex = "^[a-zA-Z\\.\\s]+$"; 
  7. Allow Only Letters and Spaces, Disallow Leading or Trailing Spaces:

    String fullName = "David Johnson"; String regex = "^[a-zA-Z]+( [a-zA-Z]+)*$"; 
  8. Java Regex to Validate Full Name with Accented Characters:

    String fullName = "Andr�� M��ller"; String regex = "^[\\p{L} .'-]+$"; 
  9. Validate Full Name with Java Regex Allowing Initials:

    String fullName = "J. K. Rowling"; String regex = "^[a-zA-Z]+( [a-zA-Z]+)*(\\.[a-zA-Z]+)*$"; 
  10. Java Regex to Allow Only Alphabets, Spaces, and Ampersand in Full Name:

    String fullName = "John & Mary"; String regex = "^[a-zA-Z&\\s]+$"; 

More Tags

2-way-object-databinding prettier landscape quaternions screencast css-transitions comments runtime distinct database-schema

More Programming Questions

More Tax and Salary Calculators

More Various Measurements Units Calculators

More Chemistry Calculators

More Investment Calculators