# Discussion on related regularity and verification in iOS application

• 2021-12-21 05:15:57
• OfStack

1. Verification regularity of mobile phone number

Regular expression:

^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}\$

Detailed explanation

Explanation:

•^...\$:
^: Start

\$: End

In the middle is the string to be processed

(13 [0-9]): The number starting with 13 and the next digit is between 0 and 9

[0-9]: Partition syntax, 13 followed by a number between 0-9

Or (or), or operation is performed on the two matching conditions before and after

• (15 [^ 4\\ D]): Beginning with 15, the next digit is 0-9 except 4

[^ 4\\ D]: All numbers from 0 to 9 except 4

^: Non-^ 4 here is any character except 4

\ D:\ D is not a number `\ ` is an escape character

• (18 [0, 2, 5-9]): A number that begins with 18 and the next 1 bit is 0 or 2 or between 5-9

•\ d {8}: 8 digits from 0 to 9
\ d: Numbers between 0 and 9

{8}: Match 8 times

Use of regular expressions:

``````
+ (BOOL)isMobileNumber:(NSString *)mobileNumber

{

// Mobile phone number to 13 ,  15 , 18 At the beginning, 8 A  \d  Numeric character

NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}\$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:mobileNumber];

}``````

2. ID card verification

``````
/**

*  ID number

* @param NSString  ID number string

* @return  Whether it is an ID number

* d{14} 14 Bit number

*/

+ (BOOL) validateIdentityCard: (NSString *)identityCard

{

BOOL flag;

if (identityCard.length <= 0) {

flag = NO;

return flag;

}

NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])\$";

NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];

return [identityCardPredicate evaluateWithObject:identityCard];

}
``````
``````
/**

*  Accurately verify the ID number

* @param IdentityNum  ID number

* @return  Is it an ID number

*/

+(BOOL)isValidWithIdentityNum:(NSString *)IdentityNum{

// Preregular matching

//......

// Calculate the last 1 Bit remainder

NSArray *arrExp = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];

NSArray *arrVaild = [NSArray arrayWithObjects:@"1", @"0", @"X", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2", nil];

long sum = 0;

for (int i = 0; i < (IdentityNum.length -1); i++) {

NSString * str = [IdentityNum substringWithRange:NSMakeRange(i, 1)];

sum += [str intValue] * [arrExp[i] intValue];

}

int idx = (sum % 11);

if ([arrVaild[idx] isEqualToString:[IdentityNum substringWithRange:NSMakeRange(IdentityNum.length - 1, 1)]]) {

return YES;

}else{

return NO;

}

return YES;

}
``````

3. Mailbox Verification

``````
/**

*  Verify mailbox

*

* @param email  Mailbox string

*

* @return  Mailbox or not

*

* [A-Z0-9a-z]  Denote  A-Z  And  0-9  And  a-z  Arbitrary 1 A

* {2,4}     Denote   Character bits greater than 2 A, less than 4 A

*/

+ (BOOL) validateEmail:(NSString *)email

{

NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

return [emailTest evaluateWithObject:email];

}
``````

4. User name verification

``````
/**

*  User name authentication

*

* @param NSString  User name string

*

* @return  User name or not

* {6,20} 6 To 20 Bit

*/

{

return B;

}
``````

``````
/**

*

*

* {6,20} 6 To 20 Bit

*/

{

}

``````

6. Verify the nickname

``````
/**

*  Validate nicknames

*

* @param NSString  Nickname string

*

* @return  Nickname or not

* {4,8} 4 To 8 Bit

*/

+ (BOOL) validateNickname:(NSString *)nickname

{

NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}\$";

NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];

}

``````

6, 4-bit verification code verification

``````
/**

* 4 Bit verification code

*

* @param verifyCode  Verification code

*

* @return  Is it a verification code

*/

+ (BOOL) validateVerifyCode:(NSString *)verifyCode

{

BOOL flag;

if (verifyCode.length <= 0) {

flag = NO;

return flag;

}

NSString *regex2 = @"^(\\d{4})\$";

NSPredicate *verifyCodePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];

return [verifyCodePredicate evaluateWithObject:verifyCode];

}
``````

Related articles: