Ios uses regular expressions to verify the mobile phone number of the password ID card

Source: Internet
Author: User

Ios uses regular expressions to verify the mobile phone number of the password ID card

In the project, we are verifying user input. The simplest method is to use regular expressions. the ios System also provides a convenient method, so that we can easily verify the user name, password, good ID card, mobile phone number, etc;

The following is a method for verifying the regular expression I used in the project,

Because it is commonly used, it is best to encapsulate it into a + static method for ease of use:

I encapsulate common methods in a Utils class using static methods and call them directly using the Class Name:

Header file:

 

/// Utils. h // AutoSizing // Created by yb on 15/2/7. // Copyright (c) 2015 http://blog.csdn.net/yangbingbinga. All rights reserved. // # import
 
  
@ Interface Utils: NSObject # pragma Regular Expression matching mobile phone number + (BOOL) checkTelNumber :( NSString *) telNumber; # pragma Regular Expression matching User Password 6-18 digits and letter combination + (BOOL) checkPassword :( NSString *) password; # pragma regular match user name, 20 characters in Chinese or English + (BOOL) checkUserName: (NSString *) userName; # pragma Regular Expression matching user ID card number + (BOOL) checkUserIdCard: (NSString *) idCard; # pragma regular horse employee number, 12-digit number + (BOOL) checkEmployeeNumber: (NSString *) number; # pragma Regular Expression matching URL + (BOOL) checkURL: (NSString *) url; @ end
 
. M implementation file:

 

 

//// Utils. m // AutoSizing /// Created by yb on 15/2/7. // Copyright (c) 2015 http://blog.csdn.net/yangbingbinga. all rights reserved. // # import Utils. h @ implementation Utils # pragma regular match 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 match user password 6-18 digits and combination of letters + (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 match user name, 20 characters in Chinese or English + (BOOL) checkUserName: (NSString *) userName {NSString * pattern = @ ^ [a-zA-Z-region] {}; NSPredicate * pred = [NSPredicate predicateWithFormat: @ self matches % @, pattern]; BOOL isMatch = [pred evaluateWithObject: userName]; return isMatch;} # pragma regular match user ID card number 15 or 18 + (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 horse employee number, 12-digit number + (BOOL) checkEmployeeNumber: (NSString *) number {NSString * pattern = @ ^ [0-9] {12 }; NSPredicate * pred = [NSPredicate predicateWithFormat: @ self matches % @, pattern]; BOOL isMatch = [pred regular: number]; return isMatch ;}# pragma regular match 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

 

Original article address: www.bkjia.com

 


Example:

 

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 error );}


 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.