Java StringValidator String Validation Example
This is an example of string validation.
import java.io.UnsupportedEncodingException;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
/**
* String validation
*
* @author kimkc
*/
public class StringValidator {
// IP address pattern
private static final Pattern PATTERN_IPADDRESS = Pattern
.compile("^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$");
// Email address pattern
private static final Pattern PATTERN_EMAIL = Pattern.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}\\@[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}(\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25})+");
// Password pattern (9 to 16 characters using a combination of letters, numbers, and special characters)
private static final Pattern PATTERN_PW = Pattern
.compile("(?=([a-zA-Z].*[0-9].*[^0-9a-zA-Z].*)|([a-zA-Z].*[^0-9a-zA-Z].*[0-9].*)|([0-9].*[a-zA-Z].*[^0-9a-zA-Z].*)|([0-9].*[^0-9a-zA-Z].*[a-zA-Z].*)|([^0-9a-zA-Z].*[a-zA-Z].*[0-9].*)|([^0-9a-zA-Z].*[0-9].*[a-zA-Z].*)$).{9,16}");
// Additional password pattern (disallows three or more repeated characters)
private static final Pattern PATTERN_SAME = Pattern.compile(".*(.)\\1\\1.*");
/**
* Checks whether the encoding is invalid.
*
* @param value
* @return
*/
public static boolean isNotValidEncoding(final String value) {
return !isValidEncoding(value);
}
/**
* Checks whether the encoding is valid.
*
* @param value
* @return
*/
public static boolean isValidEncoding(final String value) {
boolean result = true;
try {
new String("".getBytes(), value);
} catch (UnsupportedEncodingException e) {
result = false;
}
return result;
}
/**
* Checks whether the given IP address matches the regular expression.
*
* @param ip
* @return s
*/
public static boolean isIPAddress(final String ip) {
return PATTERN_IPADDRESS.matcher(ip).matches();
}
/**
* Checks whether the given email address matches the regular expression.
*
* @param email
* @return
*/
public static boolean isEmail(final String email) {
return PATTERN_EMAIL.matcher(email).matches();
}
/**
* Checks character-combination rules.
*
* @param password
* @return
*/
public static boolean isPasswordCharacter(final String password) {
return PATTERN_PW.matcher(password).matches();
}
/**
* Checks for a pattern with three or more identical characters.
*
* @param password
* @return
*/
public static boolean isSamePattern(final String password) {
return PATTERN_SAME.matcher(password).matches();
}
/**
* Checks for a pattern with three or more consecutive characters.
*
* @param password
* @return
*/
public static boolean isContinuousPattern(final String password) {
if (password == null)
return false;
int count = 0;
char c = 0;
char before = 0;
for (int i = 0; i < password.length(); i++) {
c = password.charAt(i);
if (c >= 48 && c <= 57 && (before + 1 == c)) {
count++;
if (count >= 2)
return true;
} else {
count = 0;
}
before = c;
}
return false;
}
/**
* Checks whether the regular expression is valid.
*
* @param regex
* @return
*/
public static boolean isInvalidRegularExpression(String regex) {
try {
Pattern.compile(regex);
} catch (PatternSyntaxException e) {
return false;
}
return true;
}
}