I. Purpose of writing this article
Frequently used in our projects are:
Verify your phone number,
Verify the mailbox format,
Verify that the Chinese,
Verify URLs,
Verify that it contains illegal characters,
The password 6-16-bit contains both numbers and letters,
Authentication of the identity card.
I first recorded this function code, convenient for everyone to circulate, I hope to be able to help users.
Hint: This aspect of the online resources are still relatively full, you can search by themselves. Example: IOS mobile phone number judging regular expression
Ii. Specific Content
//Verify your phone number+ (BOOL) Validateuserphone: (NSString *) str{NSString*PATTERNSTR = [NSString stringWithFormat:@"^ ((13[0-9)) | ( 14[5,7]) | (15[0-3,5-9]) | (16[6]) | (17[0,1,3,5-8]) | (18[0-9]) | (19[8,9])) \\d{8}$"]; Nsregularexpression*regularexpression =[[Nsregularexpression alloc] Initwithpattern:patternstr Options:nsregularexpressioncaseinsensitive Error : nil]; Nsuinteger Numberofmatch=[RegularExpression numberofmatchesinstring:str Optio Ns:nsmatchingreportprogress Range:nsmakerange (0, Str.length)]; if(Numberofmatch >0) {returnYES; }returnNO;}
// Verify email with domain name Verification + (BOOL) Validateemail: (NSString *) _text{ *regex=@ "[0-9a-z._%+-] [Email protected] [0-9a-z._]+\\. [A-z] {2,4} " ; *emailtest=[nspredicate Predicatewithformat:@ "selfMATCHES%@", Regex]; return [Emailtest evaluatewithobject:_text];}
// Is the Chinese + (BOOL) ischinese{ *match=@ "(^[\u4e00-\u9fa5]+$)"; *predicate = [Nspredicate predicatewithformat:@ "selfmatches%@", match]; return [predicate evaluatewithobject:self];}
//whether it is a URL+ (BOOL) Checkurl: (ID) _strinput{NSString*_strregex =@"^ (http|https|ftp)://[a-za-z0-9]+[.] [A-za-z0-9]+ ([.] [a-za-z0-9]+] {0,1} (/[a-za-z0-9-_.+=?&%]*) *$"; Nspredicate* _predicate = [Nspredicate predicatewithformat:@"Self MATCHES%@", _strregex]; if([_predicate evaluatewithobject:_strinput]) {returnYES; }Else{ returnNO; }}
// whether there is an illegal character + (BOOL) Ishaveillegalchar: (NSString *) str{ *donotwant = [Nscharacterset Charactersetwithcharactersinstring:@ ""]; = [str rangeofcharacterfromset:donotwant]; return range.location<str.length;}
//determine password 6-16-bit and include both numbers and letters+ (BOOL) Judgepasswordlegal: (NSString *) pass{BOOL result=false; if([Pass length] >=6){ //Determine If the length is greater than 6 digits and then decide whether to include both numbers and lettersNSString * regex =@"^(?! [0-9]+$) (?! [a-za-z]+$] [0-9a-za-z]{6,16}$"; Nspredicate*pred = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; Result=[pred Evaluatewithobject:pass]; } returnresult;}
//ID Number+ (BOOL) Validateidentitycard: (NSString *) identitycard{BOOL flag; if(Identitycard.length <=0) {flag=NO; returnFlag; } nsstring*regex2 =@"^ (\\d{14}|\\d{17}) (\\d|[ XX]) $"; Nspredicate*identitycardpredicate = [Nspredicate predicatewithformat:@"Self MATCHES%@", Regex2]; return[Identitycardpredicate Evaluatewithobject:identitycard];}
Cell phone number format and other judgments