Validate data using Regex in Java
This post will discuss how to validate data such as name, username, email address, phone number, date of birth, password, credit card number, etc., using regex in Java.
To validate data using a regex in Java, we need to use the Pattern and Matcher classes from the java.util.regex package. These classes allow us to define a regex pattern and match it against a given input string. We can use different regex patterns to validate different types of data, such as name, username, email address, phone number, date of birth, password, credit card number, etc. Here’s an example of how we can achieve this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import java.util.regex.Pattern; // Java program to validate data in Java class Main { private static final String NAME_REGEX = "^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$"; private static final Pattern NAME_PATTERN = Pattern.compile(NAME_REGEX); // alphanumeric and underscore are allowed private static final String USERNAME_REGEX = "^[a-zA-Z0-9_]+$"; private static final Pattern USERNAME_PATTERN = Pattern.compile(USERNAME_REGEX); // US phone number with or without dashes private static final String PHONE_REGEX = "^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$"; private static final Pattern PHONE_PATTERN = Pattern.compile(PHONE_REGEX); // local-part + @ + domain part private static final String EMAIL_REGEX = "^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*" + "@" + "(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$"; private static final Pattern EMAIL_PATTERN = Pattern.compile(EMAIL_REGEX); // 8-16 characters password with at least one digit, one lowercase letter, // one uppercase letter, one special character with no whitespaces private static final String PASSWORD_REGEX = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,16}$"; private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_REGEX); // Date in US format with support for leap years private static final String DATE_REGEX = "^(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.)31)\\1|" + "(?:(?:0?[1,3-9]|1[0-2])(\\/|-|\\.)(?:29|30)\\2))" + "(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$|^(?:0?2(\\/|-|\\.)29\\3" + "(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|" + "[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|" + "^(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.)(?:0?[1-9]|1\\d|" + "2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2})$"; private static final Pattern DATE_PATTERN = Pattern.compile(DATE_REGEX); // A valid credit card number private static final String CREDIT_CARD_REGEX = "^((4\\d{3})|(5[1-5]\\d{2})|(6011)|(7\\d{3}))" + "-?\\d{4}-?\\d{4}-?\\d{4}|3[4,7]\\d{13}$"; private static final Pattern CREDIT_CARD_PATTERN = Pattern.compile(CREDIT_CARD_REGEX); public static void main(String[] args) { String name = "Techie Delight"; String username = "techie_delight"; String DOB = "11/29/2010"; // mm/dd/yyyy String password = "Stream@Java8"; String phone = "202-555-0139"; // US Phone number String creditCard = "4024-0071-8793-4178"; if (NAME_PATTERN.matcher(name).matches() && USERNAME_PATTERN.matcher(username).matches() && DATE_PATTERN.matcher(DOB).matches() && EMAIL_PATTERN.matcher(email).matches() && PASSWORD_PATTERN.matcher(password).matches() && PHONE_PATTERN.matcher(phone).matches() && CREDIT_CARD_PATTERN.matcher(creditCard).matches()) { System.out.print("Form Data is valid"); } else { System.out.print("Form Data is invalid"); } } } |
Output:
Form Data is valid
We can also use the String.matches() method to check if a string matches a regex directly. However, this method is less efficient than using the Pattern and Matcher classes because it compiles the regex every time it is called. That’s all about validating data using regex in Java.
Also See:
Reference: OWASP Validation Regex Repository | OWASP Foundation
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)