How to validate a Username using Regular Expressions in Java Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or greater than 30 characters, then it is an invalid username. The username can only contain alphanumeric characters and underscores (_). Alphanumeric characters describe the character set consisting of lowercase characters [a - z], uppercase characters [A - Z], and digits [0 - 9]. The first character of the username must be an alphabetic character, i.e., either lowercase character [a - z] or uppercase character [A - Z]. Examples: Input: str = "1Geeksforgeeks" Output: False Explanation: The given username is invalid because it starts with a digit. Input: str = "Geeksforgeeks_21" Output: True Explanation: The given username satisfies all the conditions mentioned. Input: str = "Geeksforgeeks?10_2A" Output: False Explanation: The given username is invalid because it consists of invalid character "?". Approach: The idea is to use Regular Expressions to validate if the given username is valid or not. The following steps can be followed to compute the answer: Get the string. Form a regular expression to validate the given string. According to the conditions, the regular expression can be formed in the following way: regex = "^[A-Za-z]\\w{5, 29}$" Where: "^" represents that starting character of the string. "[A-Za-z]" makes sure that the starting character is in the lowercase or uppercase alphabet. "\\w{5, 29}" represents a check to make sure that the remaining items are word items, which includes the underscore, until it reaches the end and that is represented with $. The "{5, 29}" represents the 6-30 character constraint given to us minus the predefined first character. Match the string with the Regex. In Java, this can be done using Pattern.matcher(). Return true if the string matches with the given regex, else return false. Below is the implementation of the above approach: Java // Java program to validate a username // using Regular Expression or ReGex import java.util.regex.*; class GFG { // Function to validate the username public static boolean isValidUsername(String name) { // Regex to check valid username. String regex = "^[A-Za-z]\\w{5,29}$"; // Compile the ReGex Pattern p = Pattern.compile(regex); // If the username is empty // return false if (name == null) { return false; } // Pattern class contains matcher() method // to find matching between given username // and regular expression. Matcher m = p.matcher(name); // Return if the username // matched the ReGex return m.matches(); } // Driver Code public static void main(String[] args) { // Test Case: 1 String str1 = "Geeksforgeeks"; System.out.println(isValidUsername(str1)); // Test Case: 2 String str3 = "1Geeksforgeeks"; System.out.println(isValidUsername(str3)); // Test Case: 3 String str5 = "Ge"; System.out.println(isValidUsername(str5)); } } Output: true false false Comment More infoAdvertise with us Next Article Regular Expressions in Java P prashant_srivastava Follow Improve Article Tags : Java Practice Tags : Java Similar Reads How to validate an IP address using Regular Expressions in Java Given an IP address, the task is to validate this IP address with the help of Regular Expressions.The IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Examples: Inpu 3 min read How to Validate a Password using Regular Expressions in Android? Regular Expression basically defines a search pattern, pattern matching, or string matching. It is present in java.util.regex package. Java Regex API provides 1 interface and 3 classes. They are the following: MatchResult InterfaceMatcher classPattern classPatternSyntaxException class Pattern p = Pa 5 min read Spring - MVC Regular Expression Validation Regular Expression Validation in Spring MVC can be achieved by using Hibernate Validator which is the implementation of Bean Validation API. Hibernate Validator provides @Pattern annotation which is used for regular expression validation. Syntax: @Pattern(regex="", flag="", message="") private Strin 4 min read Regular Expressions in Java In Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressi 7 min read How to Use Regular Expression as a Substitute of endsWith() Method in Java? So primarily discuss what is endsWith() method is, so it is a method of String class that checks whether the string ends with a specified suffix. This method returns a boolean value true or false. Syntax: public boolean endsWith(String suff) Parameter: specified suffix part Return: Boolean value, he 3 min read Regular Expression to Validate a Bitcoin Address BITCOIN is a digital currency. During the digital currency transaction, a BTC address is required to verify the legality of a bitcoin wallet a Bitcoin address is a long set of alphanumeric characters that contains numbers and letters. A Bitcoin address indicates the source or destination of a Bitcoi 6 min read Like