Regular Expression for user name, password, mobile phone number, ID card (recommended), regular expression for mobile phone number

Source: Internet
Author: User

Regular Expression for user name, password, mobile phone number, ID card (recommended), regular expression for mobile phone number

I will not talk much about it. I will introduce you to using regular expressions to verify your username, password, mobile phone number, and ID card. For more information, see.

// Username + (BOOL) validateUserName :( NSString *) name {NSString * userNameRegex = @ "^ [A-Za-z0-9] {3,20} + $"; NSPredicate * userNamePredicate = [NSPredicate predicateWithFormat: @ "self matches % @", userNameRegex]; bool B = [userNamePredicate evaluateWithObject: name]; return B;} // password + (BOOL) validatePassword :( NSString *) passWord {NSString * passWordRegex = @ "^ [a-zA-Z0-9] {6, 20} + $"; NSPredicate * passWordPredicate = [NSPredicate predicateWithFormat: @ "self matches % @", passWordRegex]; return [passWordPredicate evaluateWithObject: passWord];} // determine whether the mobile phone number format is correct + (BOOL) valiMobile :( NSString *) mobile {mobile = [mobile stringByReplacingOccurrencesOfString: @ "" withString: @ ""]; if (mobile. length! = 11) {return NO;} else {/*** mobile number segment Regular Expression */NSString * CM_NUM = @ "^ (13 [4-9]) | (147) | (15 [0-178-9]) | (1705) | (18 [2-]) \ d {8} |) \ d {7} $ ";/*** Regular Expression of the UNICOM number segment */NSString * CU_NUM = @" ^ (13 [0-2]) | (145) | (15 [5-6]) | (176) | (18 [1709]) \ d {8} | () \ d {7} $ "; /*** Regular Expression of telecom number segment */NSString * CT_NUM = @ "^ (133) | (153) | (177) | (18 [, 9]) \ d {8} $ "; NSPredicate * pred1 = [NSPredicate predicateWithFormat: @" self matches % @ ", CM_NUM]; BOOL isMatch1 = [pred1 evaluateWithObject: mobile]; NSPredicate * pred2 = [NSPredicate predicateWithFormat: @ "self matches % @", CU_NUM]; BOOL isMatch2 = [pred2 ready: mobile]; NSPredicate * pred3 = [NSPredicate ready: @ "self matches % @", CT_NUM]; BOOL isMatch3 = [pred3 evaluateWithObject: mobile]; if (isMatch1 | isMatch2 | isMatch3) {return YES ;} else {return NO ;}}} /*** Method for verifying whether the ID card number is correct ** @ param IDNumber is passed into the ID card number string ** @ return YES or NO indicates whether the ID card number conforms to National Standards */+ (BOOL) isCorrect :( NSString *) IDNumber {NSMutableArray * IDArray = [NSMutableArray array]; // traverses the ID card string and stores it in the array if (IDNumber. length = 18) {for (int I = 0; I <18; I ++) {nsange range = NSMakeRange (I, 1); NSString * subString = [IDNumber substringWithRange: range]; [IDArray addObject: subString] ;}} else {for (int I = 0; I <15; I ++) {nsange range = NSMakeRange (I, 1 ); NSString * subString = [IDNumber substringWithRange: range]; [IDArray addObject: subString] ;}// coefficient array NSArray * coefficientArray = [NSArray arrayWithObjects: @ "7 ", @ "9", @ "10", @ "5", @ "8", @ "4", @ "2", @ "1", @ "6 ", @ "3", @ "7", @ "9", @ "10", @ "5", @ "8", @ "4", @ "2 ", nil]; // The remainder array NSArray * remainderArray = [NSArray arrayWithObjects: @ "1", @ "0", @ "X", @ "9", @ "8 ", @ "7", @ "6", @ "5", @ "4", @ "3", @ "2", nil]; // The sum of ID card numbers and their corresponding coefficients is obtained by multiplying them with int sum = 0; if (IDNumber. length = 18) {for (int I = 0; I <17; I ++) {int coefficient = [coefficientArray [I] intValue]; int ID = [IDArray [I] intValue]; sum + = coefficient * ID;} else {for (int I = 0; I <14; I ++) {int coefficient = [coefficientArray [I] intValue]; int ID = [IDArray [I] intValue]; sum + = coefficient * ID ;}} // NSString * str = remainderArray [(sum % 11)]; // The Last NSString * string of the ID number; if (IDNumber. length = 18) {string = [IDNumber substringFromIndex: 17];} else {string = [IDNumber substringFromIndex: 14];} // if this number is the same as the last digit of the ID card, if ([str isEqualToString: string]) {return YES;} else {return NO ;}}

The above section describes the regular expression used to verify the user name, password, mobile phone number, and ID card. I hope it will help you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.