Verification password ID card mobile number of IOS regular expression

Time:2021-9-24

Verification password ID card mobile number of IOS regular expression

In the project, we are verifying the user’s input. The simplest method is to use regular expressions. IOS system also provides very convenient methods, so that we can easily verify the user name, password, ID card, mobile phone number, etc;

The verification method of regular expression I used in the project is attached below,

As it is commonly used, you’d better package it into a + static method for easy use:

I encapsulate common methods into a utils class using static methods and call them directly with the class name:

Header file:

// 
// Utils.h 
// AutoSizing 
// 
// Created by on 15/2/7. 
//Copyright (c) 2015 http://blog.csdn.net/yangbingbinga.  All rights reserved. 
// 
 
#import <Foundation/Foundation.h> 
 
@interface Utils : NSObject 
#Pragma regular matching mobile phone number 
+ (BOOL)checkTelNumber:(NSString *) telNumber; 
#Pragma regular matching user password 6-18 digit number and letter combination 
+ (BOOL)checkPassword:(NSString *) password; 
#Pragma regular matching user name, 20 digit Chinese or English 
+ (BOOL)checkUserName : (NSString *) userName; 
#Pragma regular matching user ID number 
+ (BOOL)checkUserIdCard: (NSString *) idCard; 
#Pragma employee number, 12 digit number 
+ (BOOL)checkEmployeeNumber : (NSString *) number; 
#Pragma regular matching URL 
+ (BOOL)checkURL : (NSString *) url; 
@end

. m implementation file:

// 
// Utils.m 
// AutoSizing 
// 
// Created by on 15/2/7. 
//Copyright (c) 2015 http://blog.csdn.net/yangbingbinga.  All rights reserved. 
// 
 
#import "Utils.h" 
 
@implementation Utils 
#Pragma regular matching mobile phone 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 matching user password 6-18 digit number and letter combination 
+ (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 regular matching user name, 20 digit 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 matching user ID No. 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 employee number, 12 digit number 
+ (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

Examples of usage:

If ([utils checktelnumber: @ "18801112020") // the matching result is yes 
{ 
  Nslog (@ "phone number format is correct"); 
} 
Else // the matching result is No 
{ 
  Nslog (@ "phone number format error"); 
}

Returns 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 for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!