Created
August 11, 2014 10:09
-
-
Save sbelloz/65db82ca4d0484d0bde3 to your computer and use it in GitHub Desktop.
a class that uses static methods for validate forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* Created by SimoneBellotti on 30/04/14. | |
*/ | |
public class Validator { | |
private static Pattern pattern; | |
private static Matcher matcher; | |
/** | |
* <ul> | |
* <li>^ - Start of the line</li> | |
* <li>[a-z0-9_-] - Match characters and symbols in the list, a-z, 0-9, underscore, hyphen</li> | |
* <li>{3,15} - Length at least 3 characters and maximum length of 15</li> | |
* <li>$ - End of the line</li> | |
* </ul> | |
*/ | |
private static final String USERNAME_PATTERN = "^[a-zA-Z0-9_-]{3,15}$"; | |
// private static final String USERNAME_PATTERN = "^[a-z0-9_-]{3,30}$"; | |
/** | |
* <ul> | |
* <li>( <b>- Start of group</b></li> | |
* <li>(?=.*\d) <b>- must contains one digit from 0-9</b></li> | |
* <li>(?=.*[a-z]) <b>- must contains one lowercase characters</b></li> | |
* <li>(?=.*[A-Z]) <b>- must contains one uppercase characters</b></li> | |
* <li>(?=.*[@#$%]) <b>- must contains one special symbols in the list "@#$%"</b></li> | |
* <li>. <b>- match anything with previous condition checking</b></li> | |
* <li>{7,} <b>- length at least 7 characters</b></li> | |
* <li>) <b>- End of group</b></li> | |
* </ul> | |
*/ | |
private static final String PASSWORD_PATTERN = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{7,})"; | |
/** | |
* <ul> | |
* <li>^ - start of the line</li> | |
* <li> [_A-Za-z0-9-\\+]+ - must start with string in the bracket [ ], must contains one or more (+)</li> | |
* <li> ( - start of group #1</li> | |
* <li>\\.[_A-Za-z0-9-]+ - follow by a dot "." and string in the bracket [ ], must contains one or more (+)</li> | |
* <li> )* - end of group #1, this group is optional (*)</li> | |
* <li> @ - must contains a "@" symbol</li> | |
* <li>[A-Za-z0-9-]+ - follow by string in the bracket [ ], must contains one or more (+)</li> | |
* <li>( - start of group #2 - first level TLD checking</li> | |
* <li>\\.[A-Za-z0-9]+ - follow by a dot "." and string in the bracket [ ], must contains one or more (+)</li> | |
* <li>)* - end of group #2, this group is optional (*)</li> | |
* <li> ( - start of group #3 - second level TLD checking</li> | |
* <li> \\.[A-Za-z]{2,} - follow by a dot "." and string in the bracket [ ], with minimum length of 2</li> | |
* <li> ) - end of group #3</li> | |
* <li>$ - End of the line</li> | |
* </ul> | |
*/ | |
private static final String EMAIL_PATTERN = | |
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" | |
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
//private static final String _PHONE_NUMBER_PATTERN = | |
// "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$"; | |
private static final String PHONE_NUMBER_PATTERN = | |
"^[+][0-9]*$"; | |
// private static final String EMAIL_AND_ITALIAN_NUMBER_PATTERN = | |
// "^(([+]39)?((38[{8,9}|0])|(34[{7-9}|0])|(36[6|8|0])|(33[{3-9}|0])|(32[{8,9}]))([\\d]{7}))|" + | |
// "[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" | |
// + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
// private static final String ITALIAN_NUMBER_PATTERN = | |
// "^([+]39)?((38[{8,9}|0])|(34[{7-9}|0])|(36[6|8|0])|(33[{3-9}|0])|(32[{8,9}]))([\\d]{7})"; | |
private static final String PROVVISORY_PASSWORD_PATTERN = "((?=.*[a-z]).{4,})"; | |
/** | |
* Validate username with regular expression | |
* @param username username for validation | |
* @return true valid username, false invalid username | |
* @see #USERNAME_PATTERN | |
*/ | |
public static boolean validateUsername(final String username){ | |
pattern = Pattern.compile(USERNAME_PATTERN); | |
matcher = pattern.matcher(username); | |
return matcher.matches(); | |
} | |
/** | |
* Validate password with regular expression | |
* @param password username for validation | |
* @return true valid password, false invalid password | |
* @see #PASSWORD_PATTERN | |
*/ | |
public static boolean validatePassword(final String password){ | |
pattern = Pattern.compile(PASSWORD_PATTERN); | |
matcher = pattern.matcher(password); | |
return matcher.matches(); | |
} | |
/** | |
* Validate email with regular expression | |
* @param email email for validation | |
* @return true valid email, false invalid email | |
* @see #EMAIL_PATTERN | |
*/ | |
public static boolean validateEmail(final String email) { | |
pattern = Pattern.compile(EMAIL_PATTERN); | |
matcher = pattern.matcher(email); | |
return matcher.matches(); | |
} | |
/** | |
* Validate phone numbers eg: 1234567890 | |
* @param number number for validation | |
* @return true valid number, false invalid number | |
* @see #PHONE_NUMBER_PATTERN | |
*/ | |
public static boolean validatePhoneNumber(final String number) { | |
pattern = Pattern.compile(PHONE_NUMBER_PATTERN); | |
matcher = pattern.matcher(number); | |
return matcher.matches(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment