Java – Date Validation with Regular Expression

Date Format (dd/mm/yyyy) Regular Expression Pattern

(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)

The above regular expression is used to validate the date format in “dd/mm/yyyy”, you can easy customize to suit your need. However, it’s a bit hard to validate the leap year, 30 or 31 days of a month, we may need basic logic as below.

[code lang=”java”]import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateValidator {

private Pattern pattern;
private Matcher matcher;

private static final String DATE_PATTERN =
"(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";

public DateValidator() {
pattern = Pattern.compile(DATE_PATTERN);
}

/**
* Validate date format with regular expression
* @param date date address for validation
* @return true valid date fromat, false invalid date format
*/
public boolean validate(final String date) {

matcher = pattern.matcher(date);

if(matcher.matches()) {

matcher.reset();

if(matcher.find()) {

String day = matcher.group(1);
String month = matcher.group(2);
int year = Integer.parseInt(matcher.group(3));

if (day.equals("31") &&
(month.equals("4") || month .equals("6") || month.equals("9") ||
month.equals("11") || month.equals("04") || month .equals("06") ||
month.equals("09"))) {
return false; // only 1,3,5,7,8,10,12 has 31 days
} else if (month.equals("2") || month.equals("02")) {
//leap year
if(year % 4==0) {
if(day.equals("30") || day.equals("31")) {
return false;
} else {
return true;
}
} else {
if(day.equals("29")||day.equals("30")||day.equals("31")) {
return false;
} else {
return true;
}
}
} else {
return true;
}
} else {
return false;
}
} else {
return false;
}
}
}[/code]

Java – Password Validation with Regular Expression

Password Regular Expression Pattern:

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})

Description:

( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[A-Z]) # must contains one uppercase characters
(?=.*[@#$%]) # must contains one special symbols in the list “@#$%”
. # match anything with previous condition checking
{6,20} # length at least 6 characters and maximum of 20
) # End of group

?= – means apply the assertion condition, meaningless by itself, always work with other combination

Whole combination is means, 6 to 20 characters string with at least one digit, one upper case letter, one lower case letter and one special symbol (“@#$%”). This regular expression pattern is very useful to implement a strong and complex password.

[code lang=”java”]import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordValidator {

private Pattern pattern;
private Matcher matcher;

private static final String PASSWORD_PATTERN =
"((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})";

public PasswordValidator() {
pattern = Pattern.compile(PASSWORD_PATTERN);
}

/**
* Validate password with regular expression
* @param password password for validation
* @return true valid password, false invalid password
*/
public boolean validate(final String password) {

matcher = pattern.matcher(password);
return matcher.matches();

}

}[/code]

Java – Username Validation with Regular Expression

Username Regular Expression Pattern:

^[a-z0-9_-]{3,15}$

Description:

^    # Start of the line
[a-z0-9_-] # Match characters and symbols in the list, a-z, 0-9, underscore, hyphen
{3,15} # Length at least 3 characters and maximum length of 15
$ # End of the line

Example:
[code lang=”java”]import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UsernameValidator {

private Pattern pattern;
private Matcher matcher;

private static final String USERNAME_PATTERN = "^[a-z0-9_-]{3,15}$";

public UsernameValidator() {
pattern = Pattern.compile(USERNAME_PATTERN);
}

/**
* Validate username with regular expression
* @param username username for validation
* @return true valid username, false invalid username
*/
public boolean validate(final String username) {

matcher = pattern.matcher(username);
return matcher.matches();

}
}[/code]