Regular expressions make it easy to match data or perform more complex string validation.
Here are some common fields of validation, summed up, will always be used in the future.
1 ImportJava.util.regex.Matcher;2 ImportJava.util.regex.Pattern;3 4 Public classValidate {5 //a check on birthdays6 //format: Yyyy-mm-dd7 Public Staticstring Isbirthday (string birthday) {8String errormsg= "";9String pat= "\\d{4}-\\d{1,2}-\\d{1,2}";TenPattern p=Pattern.compile (PAT); OneMatcher m=P.matcher (birthday); A if(M.matches ()) { -errormsg= "Birthday format valid."; -}Else{ theerrormsg= "Birthday format is not legal."; - } - returnerrormsg; - } + - //The verification of the account + //format: Start of letter, allow 6-12 bytes, allow alphanumeric underline A Public Staticstring Isusername (string username) { atString errormsg= ""; -String pat= "[a-za-z][a-za-z0-9_]{5,11}"; -Pattern p=Pattern.compile (PAT); -Matcher m=P.matcher (username); - if(M.matches ()) { -errormsg= "account format is legal."; in}Else{ -errormsg= "account format is not legal."; to } + returnerrormsg; - } the * //The check of QQ number $ //format: Starting from 10000 5-11 bitPanax Notoginseng Public Staticstring Isqqnumber (String qqnumber) { -String errormsg= ""; theString pat= "[1-9][0-9]{4,10}"; +Pattern p=Pattern.compile (PAT); AMatcher m=P.matcher (qqnumber); the if(M.matches ()) { +errormsg= "QQ number format is legal."; -}Else{ $errormsg= "QQ number format is not legal"; } $ returnerrormsg; - } - the //verification of the mobile phone number - //format: 13 145 147 15 18 StartWuyi Public Staticstring Isphonenumber (String phonenumber) { theString errormsg= ""; -String pat= "(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8| 9]|18[0|1|2|3|5|6|7|8| 9]) \\d{8} "; WuPattern p=Pattern.compile (PAT); -Matcher m=P.matcher (phonenumber); About if(M.matches ()) { $errormsg= "mobile phone number format is legal."; -}Else{ -errormsg= "mobile phone number format is not legal."; - } A returnerrormsg; + } the - //verification of the ID card number $ Public Staticstring Isidnumber (String idnumber) { theString errormsg= ""; theString pat= "\\d{15}|\\d{18}"; thePattern p=Pattern.compile (PAT); theMatcher m=P.matcher (idnumber); - if(M.matches ()) { inerrormsg= "ID number format is legal."; the}Else{ theerrormsg= "ID number format is not valid."; About } the returnerrormsg; the } the + //verification of e-mail - //format: [email protected] the Public Staticstring isaddress (string address) {BayiString errormsg= ""; theString pat= "\\w+ ([-+.] \\w+) *@\\w+ ([-.] \\w+) *\\.\\w+ ([-.] \\w+) * "; thePattern p=Pattern.compile (PAT); -Matcher m=P.matcher (address); - if(M.matches ()) { theerrormsg= "e-mail format is legal."; the}Else{ theerrormsg= "e-mail format is not legal."; the } - returnerrormsg; the } the the //Check for phone numbers94 //format: xxx-xxxxxxxx or xxxx-xxxxxxx the Public Staticstring Isphone (String phone) { theString errormsg= ""; theString pat= "\\d{3}-\\d{8}|\\d{4}-\\d{7}";98Pattern p=Pattern.compile (PAT); AboutMatcher m=p.matcher (phone); - if(M.matches ()) {101errormsg= "Phone number format is legal.";102}Else{103errormsg= "Phone number format is not legal.";104 } the returnerrormsg;106 }107 108 //The verification of the postal code109 //China postcode is 6 digits the Public Staticstring Ispostnumber (String postnumber) {111String errormsg= ""; theString pat= "[1-9]\\d{5} (?! \\d) ";113Pattern p=Pattern.compile (PAT); theMatcher m=P.matcher (postnumber); the if(M.matches ()) { theerrormsg= "Postal code format is legal.";117}Else{118errormsg= "Postal code format is not valid.";119 } - returnerrormsg;121 }122 123 //verification of Chinese names124 //There are more than two Chinese the Public Staticstring Ischinesename (String chinesename) {126String errormsg= "";127String pat= "[\u4e00-\u9fa5]{2,}"; -Pattern p=Pattern.compile (PAT);129Matcher m=P.matcher (chinesename); the if(M.matches ()) {131errormsg= "Chinese name format is valid."; the}Else{133errormsg= "Chinese name format is not valid.";134 }135 returnerrormsg;136 }137 138 //Verification of the website139 Public Staticstring Iswebsite (String website) { $String errormsg= "";141String pat= "http://([\\w-]+\\.) +[\\w-]+ (/[\\w-./?%&=]*)? $ ";142Pattern p=Pattern.compile (PAT);143Matcher m=P.matcher (website);144 if(M.matches ()) {145errormsg= "website format is legal.";146}Else{147errormsg= "website format is not valid.";148 }149 returnerrormsg; Max}
151}
[Regular expression] validation of common fields