Java Regular Expression Form validation class tool class (verify mailbox, mobile phone number, QQ number, etc.) _java

Source: Internet
Author: User

Java uses regular expressions for form validation tools class, can verify mailbox, mobile phone number, QQ number, etc.

Copy Code code as follows:

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);
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.