How to validate a Username using Regular Expressions in Java Last Updated : 16 Oct, 2020 Comments Improve Suggest changes 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 How to validate a Username using Regular Expressions in Java prashant_srivastava Follow Improve Article Tags : Java Algorithms DSA Practice Tags : AlgorithmsJava Similar Reads How to validate ISIN using Regular Expressions ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must sati 6 min read 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 PAN Card number using Regular Expression Given string str of alphanumeric characters, the task is to check whether the string is a valid PAN (Permanent Account Number) Card number or not by using Regular Expression.The valid PAN Card number must satisfy the following conditions: It should be ten characters long.The first five characters sh 6 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 How to validate CVV number using Regular Expression Given string str, the task is to check whether it is a valid CVV (Card Verification Value) number or not by using Regular Expression. The valid CVV (Card Verification Value) number must satisfy the following conditions: It should have 3 or 4 digits.It should have a digit between 0-9.It should not ha 5 min read How to validate pin code of India using Regular Expression Given a string of positive number ranging from 0 to 9, the task is to check whether the number is valid pin code or not by using a Regular Expression. The valid pin code of India must satisfy the following conditions. It can be only six digits.It should not start with zero.First digit of the pin cod 6 min read How to validate Indian Passport number using Regular Expression Given a string str of alphanumeric characters, the task is to check whether the given string is a valid passport number or not by using Regular Expression. A valid passport number in India must satisfy the following conditions: It should be eight characters long.The first character should be an uppe 5 min read Regular Expressions to Validate Google Analytics Tracking Id Given some Google Analytics Tracking IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid Tracking Id are: It is an alphanumeric string i.e., containing digits (0-9), alphabets (A-Z), and a Special character hyphen(-).The hyphen will come in between the g 5 min read Validating UPI IDs using Regular Expressions Given some UPI IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid UPI ID: UPI ID is an alphanumeric String i.e., formed using digits(0-9), alphabets (A-Z and a-z), and other special characters.It must contain '@'.It should not contain whitespace.It may 5 min read Like