iOS uses common regular expressions to verify password ID phone number

Source: Internet
Author: User

In the project, we are validating the user's input, the simplest way is to use regular expression, iOS system also provides a very convenient way, so that we can easily verify the user name, password, id good, mobile phone number, etc.

Here is a validation method for the regular expression I used in my project,

Due to the more commonly used, it is best to encapsulate a static method, easy to use:

I use the usual methods, which are encapsulated in a Utils class using static methods, and can be called 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 <Foundation/Foundation.h> @interface utils:nsobject#pragma Regular Match phone number + (BOOL) Checktelnumber: (NSString *) Telnumber; #pragma regular match user password 6-18 digit and letter combination + (BOOL) Checkpassword: (NSString *) password;# pragma match user name, 20-bit Chinese or English + (BOOL) Checkusername: (NSString *) userName; #pragma regular match User ID number + (BOOL) Checkuseridcard: ( NSString *) Idcard; #pragma regular employee number, 12 digit + (BOOL) Checkemployeenumber: (NSString *) number; #pragma regular match 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 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-bit 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 match user name, 20-bit 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 bit + (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 employee number, 12 digit + (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 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 address: Http://blog.csdn.net/yangbingbinga


Examples of Use methods:

    if ([Utils checktelnumber:@ "18801112020"])  //Match result, yes    {        NSLog (@ "Phone number format correct");    }    else                                        //Match result is no    {        NSLog (@ "Phone number format error");    }



iOS uses common regular expressions to verify password ID phone number

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.