Create a new string Category: NSString (Check), defining class methods is more convenient
. h file
@interface nsstring (Check) // Verify user name input must start with a letter + (BOOL) Checkusername: (NSString *) userName Showwaininginview: (UIView *) view; // Verify user name input must be phone number + (BOOL) Checktelphone: (NSString *) Telphone Showwaininginview: (UIView *) view; // Verify that the user name is a normal account + (BOOL) Isusername: (NSString *) userName; // Verify that the user name is phone number + (BOOL) Istelphone: (NSString *) Telphone; @end
. m file
#import "nsstring+check.h"@implementationnsstring (Check)//You must start with a letter when verifying user name input+ (BOOL) Checkusername: (NSString *) userName Showwaininginview: (UIView *) view{//decide whether to start with a letterNSString *regex =@"^[a-za-z][a-za-z0-9]*$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:username]; if(!IsMatch) {[Mbprogresshud showerror:@"user name must start with a letter"Toview:view]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Mbprogresshud hidehudforview:view]; }); returnNO; } returnYES;}//Verify that the user name is entered with a phone number+ (BOOL) Checktelphone: (NSString *) Telphone Showwaininginview: (UIView *) view{NSString*regex =@"^[1][358][0-9]{9}$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:telphone]; if(!IsMatch) {[Mbprogresshud showerror:@"cell phone number is not formatted correctly"Toview:view]; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (1.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{[Mbprogresshud hidehudforview:view]; }); returnNO; } returnYES;}//Verify that the user name is a normal account+ (BOOL) Isusername: (NSString *) username{NSString*regex =@"^[a-za-z][a-za-z0-9]*$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:username]; if(!IsMatch) { returnNO; } returnYES;}//Verify that the user name is a phone number+ (BOOL) Istelphone: (NSString *) telphone{NSString*regex =@"^[1][358][0-9]{9}$"; Nspredicate*pre = [Nspredicate predicatewithformat:@"Self MATCHES%@", regex]; BOOL IsMatch=[Pre evaluatewithobject:telphone]; if(!IsMatch) { returnNO; } returnYES;}@end
IOS: A regular expression that determines whether a user name starts with a letter and the phone number is entered correctly