IOS regular expression verification password ID card mobile number
In the project, the easiest way we are verifying user input is to use regular expressions. The ios system also provides a very convenient method, allowing us to easily verify username, password, ID card, mobile phone number, etc.;
Below is the verification method of the regular expression I used in the project.
Because it is more commonly used, it is best to encapsulate it into + static method for easy use:
I encapsulate the commonly used methods into a Utils class using static methods and call them directly using the class name:
Header file:
// // // AutoSizing // // Created by on 15/2/7. // Copyright (c) 2015 /yangbingbinga. All rights reserved.// #import <Foundation/> @interface Utils : NSObject #pragma regular matching mobile number+ (BOOL)checkTelNumber:(NSString *) telNumber; #pragma regular match user password 6-18 digits and letter combinations+ (BOOL)checkPassword:(NSString *) password; #pragma Regularly match user name, 20 digits Chinese or English+ (BOOL)checkUserName : (NSString *) userName; #pragma regular matching user ID number+ (BOOL)checkUserIdCard: (NSString *) idCard; #pragma regular matching employee number, 12 digits+ (BOOL)checkEmployeeNumber : (NSString *) number; #pragma regular matching URL+ (BOOL)checkURL : (NSString *) url; @end
.m implementation file:
// // // AutoSizing // // Created by on 15/2/7. // Copyright (c) 2015 /yangbingbinga. All rights reserved.// #import "" @implementation Utils #pragma regular matching mobile number+ (BOOL)checkTelNumber:(NSString *) telNumber { NSString *pattern = @"^1+[3578]+\\d{9}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:telNumber]; return isMatch; } #pragma regular match user password 6-18 digits and letter combinations+ (BOOL)checkPassword:(NSString *) password { NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:password]; return isMatch; } #pragma Regularly match user name, 20 digits Chinese or English+ (BOOL)checkUserName : (NSString *) userName { NSString *pattern = @"^[a-zA-Z\u4E00-\u9FA5]{1,20}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:userName]; return isMatch; } #pragma regular match user ID number 15 or 18 digits+ (BOOL)checkUserIdCard: (NSString *) idCard { NSString *pattern = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:idCard]; return isMatch; } #pragma regular matching employee number, 12 digits+ (BOOL)checkEmployeeNumber : (NSString *) number { NSString *pattern = @"^[0-9]{12}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:number]; return isMatch; } #pragma regular matching URL+ (BOOL)checkURL : (NSString *) url { NSString *pattern = @"^[0-9A-Za-z]{1,50}"; NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern]; BOOL isMatch = [pred evaluateWithObject:url]; return isMatch; } @end
Example of usage:
if ([Utils checkTelNumber:@"18801112020"]) //The matching result is YES{ NSLog(@"Mobile phone number format is correct"); } else //The matching result is NO{ NSLog(@"Mobile phone number format"); }
Return a boolean value. According to the boolean value, we can do the corresponding operation.
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!