Java common Regular Expression Verification

Source: Internet
Author: User

Private Static final logger log = loggerfactory. getlogger (datautil. Class );
/**
* Verify that the mobile phone number is valid
* @ Param Phone: mobile phone number
* @ Return true: Valid, false, illegal
*/
Public static Boolean validatephone (string phone ){
Boolean flag = false;
String Reg = "^ (13 [0-9]) | (15 [0-9]) | (14 [0-9]) | (18 [0-9]) \ D {8} $ ";
Pattern pattern = pattern. Compile (REG );
Flag = pattern. matcher (phone). Matches ();
Return flag;
}

/**
* Verify that the email address is valid.
* @ Param email address
* @ Return true: Valid, false is invalid
*/
Public static Boolean validateemail (string email ){
Boolean flag = false;
String Reg = "^ [a-zA-Z0-9] * [-_]? [A-zA-Z0-9] +) * @ ([a-zA-Z0-9] * [-_]? [A-zA-Z0-9] +) + [\.] [A-Za-Z] {2, 3} ([\.] [A-Za-Z] {2 })? $ ";
Pattern pattern = pattern. Compile (REG );
Flag = pattern. matcher (email). Matches ();
Return flag;
}

/**
* Verify that the phone number format is correct.
* @ Param telephone
* @ Return true: the format is correct. False: The format is incorrect.
*/
Public static Boolean validatetelephone (string telephone ){
Boolean flag = false;
String Reg = "(^ (\ + 86 \-| 86 \\-)? [0-9] {3} [-] \ D {8}) | (^ (\ + 86 \-| 86 \\-)? [0-9] {4} [-] \ D {7 })";
Pattern pattern = pattern. Compile (REG );
Flag = pattern. matcher (telephone). Matches ();
Return flag;
}

/**
* Verify that the zip code meets the requirements
* @ Param STR zip code
* @ Return true: the zip code is correct. False: the zip code is incorrect.
*/
Public static Boolean validatezip (string Str ){
Boolean flag = false;
String Reg = "^ ([0-9]) \ D {5 }";
Pattern pattern = pattern. Compile (REG );
Flag = pattern. matcher (fax). Matches ();
Return flag;
}

 

/**
* Verify the MSN account
* @ Param STR: MSN account
* @ Return true indicates MSN; false indicates non-msn
*/
Public static Boolean validatemsn (string Str ){
Boolean flag = false;
String Reg = "^ [a-zA-Z0-9] * [-_]? [A-zA-Z0-9] +) * @ (live.com | hotmail.com | live.cn )";
Pattern pattern = pattern. Compile (REG );
Flag = pattern. matcher (STR). Matches ();
Return flag;
}

 

/**
* Verify that the QQ number is valid.
* @ Param str qq number
* @ Return true: QQ number; false: Not QQ number
*/
Public static Boolean validateqq (string Str ){
Boolean flag = false;
String Reg = "^ [1-9] [0-9] {4, 9} $ ";
Pattern pattern = pattern. Compile (REG );
Flag = pattern. matcher (STR). Matches ();
Return flag;
}

 

/**
* Verify that the ID card number format meets the requirements
* @ Param cardid ID
* @ Return true: the ID card meets the requirements. False: the ID card does not meet the requirements.
*/
Public static Boolean validateidcard (string cardid ){
Boolean flag = false;
String Reg = "(^ \ D {15} $) | (\ D {17 }(? : \ D | x) $ )";
Pattern pattern = pattern. Compile (REG );
Flag = pattern. matcher (cardid). Matches ();
Return flag;
}

 

/**
* Verify that the nickname meets the requirements (cannot be a pure number, cannot contain @ characters, cannot start with a number)
* @ Param STR nickname
* @ Return true the nickname is valid. False the nickname is invalid.
*/
Public static Boolean validatenickname (string Str ){
Boolean flag = false;
String numreg = "([\ u4e00-\ u9fa5] + [^ @] + $) | (^ [^ @ 0-9] + ([\ u4e00-\ u9fa5] + [^ @] + | [^ @] + [A-Za-Z _] + | [^ @] + \ D +) [^ @] + $ )";
Pattern pattern = pattern. Compile (numreg );
Flag = pattern. matcher (STR). Matches ();
Return flag;
}

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.