Objective
We all know that development has been inseparable from a number of common verification formats, such as: mailbox, mobile phone number, etc., in the development process will generally create a new tool class, specialized management of these verification methods, simple development process. Generally use the form of regular expressions to make judgments, this article enumerates some of the projects are very useful for some regular expression of the judgment statement, later developed directly copy and paste can be, so that greatly save the development time, the following not much to say, directly on the code.
First, verify mobile phone number:
+ (BOOL) IsMobile: (nsstring*) Mobile
{
NSString *regex = @ "^1+[34578]+\d{9}";
Nspredicate *pred = [nspredicate predicatewithformat:@ "SELF matches%@", regex);
return [pred evaluatewithobject:mobile];
}
Second, the mailbox verification:
+ (BOOL) Isemail: (nsstring*) Email
{
NSString *regex = @ "^[\\w-]+ (\\.[ \\w-]+) *@[\\w-]+ (\\.[ \\w-]+) +$ ";
Nspredicate *pred = [nspredicate predicatewithformat:@ "SELF matches%@", regex);
return [pred evaluatewithobject:email];
}
Third, the plastic test
+ (BOOL) Ispureint: (nsstring*) string{
nsscanner* scan = [Nsscanner scannerwithstring:string];
int Val;
Return[scan Scanint:&val] && [scan isatend];
}
Four, float type verification
-(BOOL) Ispurefloat: (nsstring*) string{
nsscanner* scan = [Nsscanner scannerwithstring:string];
float Val;
Return[scan Scanfloat:&val] && [scan isatend];
}
Five, Chinese verification
+ (BOOL) Ischinese: (nsstring*) Chinese
{
nsstring *regex = @ "^[\u4e00-\u9fa5]";
Nspredicate *pred = [nspredicate predicatewithformat:@ "SELF matches%@", regex);
return [pred Evaluatewithobject:chinese];
}
Vi. URL Verification of URLs
+ (BOOL) Isurl: (nsstring*) URL
{
nsstring *regex = @ "[a-za-z]+://[^\s]*";
Nspredicate *pred = [nspredicate predicatewithformat:@ "SELF matches%@", regex);
return [pred Evaluatewithobject:url];
}
Vii. Verification of identification number (more cumbersome)
+ (BOOL) Verifyidcardnumber: (NSString *) idcardnumber {idcardnumber = [Idcardnumber stringbytrimmingcharactersinset:[
Nscharacterset Whitespaceandnewlinecharacterset]];
if ([Idcardnumber length]!=) {return NO; } nsstring *MMDD = @ "((0[13578]|1[02)) (0[1-9]|[ 12][0-9]|3[01]) | ((0[469]|11) (0[1-9]|[ 12][0-9]|30)) | (02 (0[1-9]|[
1][0-9]|2[0-8])) ";
NSString *LEAPMMDD = @ "0229";
NSString *year = @ "(19|20) [0-9]{2}"; NSString *leapyear = @ "(19|20) (0[48]|[ 2468][048]| [13579]
[26]) ";
NSString *YEARMMDD = [NSString stringwithformat:@ "%@%@", year, MMDD];
NSString *LEAPYEARMMDD = [NSString stringwithformat:@ "%@%@", Leapyear, LEAPMMDD]; NSString *YYYYMMDD = [NSString stringwithformat:@ "(%@) | ( %@)|
(%@)) ", YEARMMDD, LEAPYEARMMDD, @" 20000229 "]; NSString *area = @ "(1[1-5]|2[1-3]|3[1-7]|4[1-6]|5[0-4]|6[1-5]|82|[
7-9]1) [0-9]{4} ";
NSString *regex = [NSString stringwithformat:@ "%@%@%@", area, YYYYMMDD, @ "[0-9]{3}[0-9xx]"]; Nspredicate *regextest = [nspredicate predicatewithformat:@ SELF MatcheS%@ ", regex]; if (![ Regextest Evaluatewithobject:idcardnumber]) {return NO;} int summary = ([Idcardnumber substringwithrange:nsmakerange (0 , 1)].intvalue + [Idcardnumber substringwithrange:nsmakerange (10,1)].intvalue) *7 + ([idcardnumber substringWithRange: Nsmakerange (1,1)].intvalue + [Idcardnumber substringwithrange:nsmakerange (11,1)].intvalue) *9 + ([IDCardNumber Substringwithrange:nsmakerange (2,1)].intvalue + [Idcardnumber substringwithrange:nsmakerange (12,1)].intValue) *10 + ([Idcardnumber Substringwithrange:nsmakerange (3,1)].intvalue + [Idcardnumber substringwithrange:nsmakerange (13,1)] . intvalue) *5 + ([Idcardnumber substringwithrange:nsmakerange (4,1)].intvalue + [Idcardnumber substringWithRange: Nsmakerange (14,1)].intvalue) *8 + ([Idcardnumber substringwithrange:nsmakerange (5,1)].intvalue + [IDCardNumber Substringwithrange:nsmakerange (15,1)].intvalue) *4 + ([Idcardnumber substringwithrange:nsmakerange (6,1)].intValue + [Idcardnumber Substringwithrange:nsmakerange (16,1)].intvalue) *2 + [Idcardnumber substringwithrange:nsmakerange (7,1)].intvalue *1 + [Idcardnumber substringwithrange:nsmakerange (
8,1)].intvalue *6 + [Idcardnumber substringwithrange:nsmakerange (9,1)].intvalue *3;
Nsinteger remainder = Summary% 11;
NSString *checkbit = @ "";
NSString *checkstring = @ "10x98765432"; Checkbit = [checkstring substringwithrange:nsmakerange (remainder,1)];//judge check bit return [Checkbit isequaltostring:[[
Idcardnumber Substringwithrange:nsmakerange (17,1)] uppercasestring]]; }
Eight, the general user name verification
+ (BOOL) Isusername: (NSString *) name
{
NSString *usernameregex = @ "^[a-za-z0-9]{6,20}+$";
Nspredicate *usernamepredicate = [nspredicate predicatewithformat:@ "SELF matches%@", Usernameregex]; return
[ Usernamepredicate evaluatewithobject:name];
}
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.