iOS 正则表达式验证

//
//  ZFValidator.h
//  MyStudyDemo
//
//  Created by FranZhou on 16/7/18.
//  Copyright © 2016年 kimree. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ZFValidator : NSObject

/**
 *  验证邮箱格式
 *
 *  @param email
 *
 *  @return
 */
+ (BOOL)isEmail:(NSString *)email;

/**
 *  验证手机号
 *
 *  @param phoneNum
 *
 *  @return
 */
+ (BOOL)isMobilePhoneNumber:(NSString *)phoneNum;

/**
 *  验证座机号码
 *
 *  @param telephoneNum
 *
 *  @return
 */
+ (BOOL)isTelephoneNumber:(NSString *)telephoneNum;

/**
 *  验证身份证号码
 *
 *  @param idNum
 *
 *  @return
 */
+ (BOOL)isIdentityNumber:(NSString *)idNum;

/**
 *  验证密码(数字字母组合,不能纯数字或纯字母)
 *
 *  @param password
 *
 *  @return
 */
+ (BOOL)isPassword:(NSString *)password;


/**
 *  字符串验证
 *
 *  @param str       要验证的字符串
 *  @param regexpStr 正则表达式
 *
 *  @return
 */
+(BOOL)validateString:(NSString *) str withRegexp:(NSString *)regexpStr;

@end

  

 

ZFValidator.m
//
//  ZFValidator.m
//  MyStudyDemo
//
//  Created by FranZhou on 16/7/18.
//  Copyright © 2016年 kimree. All rights reserved.
//

#import "ZFValidator.h"

@implementation ZFValidator

/**
 *  验证邮箱格式
 *
 *  @param email
 *
 *  @return
 */
+(BOOL)isEmail:(NSString *)email{
    // 去空格处理
    NSString *validatorStr = [email stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *regexpStr = @"\\w+(.\\w+)*@\\w+.\\w+";
    return [self validateString:validatorStr withRegexp:regexpStr];
}

/**
 *  验证手机号
 *
 *  @param phoneNum
 *
 *  @return
 */
+ (BOOL)isMobilePhoneNumber:(NSString *)phoneNum{
    NSString *validatorStr = [phoneNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *regexpStr = @"^(1[3|4|5|8|7])[\\d]{9}$";
    return [self validateString:validatorStr withRegexp:regexpStr];
}


/**
 *  验证座机号码
 *
 *  @param telephoneNum
 *
 *  @return
 */
+ (BOOL)isTelephoneNumber:(NSString *)telephoneNum{
    NSString *validatorStr = [telephoneNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *regexpStr = @"^(0\\d{2,3}-?)?\\d{7,8}$";
    return [self validateString:validatorStr withRegexp:regexpStr];
}

/**
 *  验证密码
 *
 *  @param password
 *
 *  @return
 */
+ (BOOL)isPassword:(NSString *)password{
    NSString *validatorStr = [password stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *regexpStr = @"([A-Za-z]+\\d+\\w*)|(\\d+[A-Za-z]+\\w*)";
    return [self validateString:validatorStr withRegexp:regexpStr];
}

/**
 *  验证身份证号码
 *
 *  @param idNum
 *
 *  @return
 */
+ (BOOL)isIdentityNumber:(NSString *)idNum{
    NSString *validatorStr = [idNum stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *regexpStr = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
    return [self validateString:validatorStr withRegexp:regexpStr];
}

/**
 *  字符串验证
 *
 *  @param str       要验证的字符串
 *  @param regexpStr 正则表达式
 *
 *  @return
 */
+(BOOL)validateString:(NSString *) str withRegexp:(NSString *)regexpStr{
    BOOL isMatch = NO;
    
    NSError *error = nil;
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:regexpStr options:NSRegularExpressionAnchorsMatchLines error:&error];
    if (error == nil) {
        NSArray<NSTextCheckingResult *> *matches = [regex matchesInString:str options:NSMatchingAnchored range:NSMakeRange(0, str.length)];
        for (NSTextCheckingResult *match in matches) {
            NSRange range = match.range;
            
            // 全匹配才算满足验证
            if (range.location == 0 && range.length == str.length) {
                isMatch = YES;
                break;
            }
        }
    }
    
    return isMatch;
}

@end

  

posted @ 2016-06-28 10:50  大雨不晴  阅读(251)  评论(0编辑  收藏  举报