Java uses regular expressions for form validation tools class, can verify mailbox, mobile phone number, QQ number, etc.
Package util;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;
/**
* Use regular expressions for form validation
*
*/
public class Regexvalidateutil {
Static Boolean flag = FALSE;
static String regex = "";
public static Boolean check (String str, string regex) {
try {
Pattern pattern = pattern.compile (regex);
Matcher Matcher = Pattern.matcher (str);
Flag = Matcher.matches ();
catch (Exception e) {
Flag = false;
}
return flag;
}
/**
* Verify non-null
*
* @param Email
* @return
*/
public static Boolean checknotemputy (String notemputy) {
Regex = "^\\s*$";
Return check (notemputy, regex)? False:true;
}
/**
* Verify Mailbox
*
* @param Email
* @return
*/
public static Boolean checkemail (String email) {
String regex = "^\\w+[-+.] \\w+) *@\\w+ ([-.] \\w+) *\\.\\w+ ([-.] \\w+) *$ ";
return check (email, regex);
}
/**
* Verify mobile phone number
*
* Mobile Number: 139, 138, 137, 136, 135, 134, 150, 151, 152, 157, 158, 159, 182, 183, 187, 188, 147
* Unicom Number: 130, 131, 132, 136, 185, 186, 145
* Telecom Number: 133, 153, 180, 189
*
* @param cellphone
* @return
& nbsp */
public static Boolean Checkcellphone (String cellphone) {
string regex = "^ ((13[0-9)) | ( 14[5|7]) | (15 ([0-3]|[ 5-9]) | (18[0,5-9]) \\d{8}$ ";
return Check (cellphone, regex);
}
/**
* Verify fixed number
*
* @param telephone
* @return
*/
public static Boolean Checktelephone (String telephone) {
string regex = ' ^ (0\\d{2}-\\d{8} (-\\d{1,4})?) | (0\\d{3}-\\d{7,8} (-\\d{1,4})?) $";
return check (telephone, regex);
}
/**
* Verify Fax number
*
* @param Fax
* @return
*/
public static Boolean Checkfax (String fax) {
String regex = "^ (0\\d{2}-\\d{8} (-\\d{1,4})?) | (0\\d{3}-\\d{7,8} (-\\d{1,4})?) $";
Return check (fax, regex);
}
/**
* Verify QQ number
*
* @param QQ
* @return
*/
public static Boolean checkqq (String QQ) {
String regex = "^[1-9][0-9]{4,} $";
Return check (QQ, regex);
}
}