zoukankan      html  css  js  c++  java
  • 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
    

      

  • 相关阅读:
    C# 中的委托和事件
    Sql Server 中一个非常强大的日期格式化函数
    SQL中合并多行记录的方法总汇
    浏览器Quirksmode(怪异模式)与标准模式
    常用的Mysql数据库操作语句大全
    mysql sql语句大全
    MySQL 最基本的SQL语法/语句
    仿微信聊天面板制作 javascript
    $.ajax()方法详解
    正则表达式匹配电话号码和手机号码
  • 原文地址:https://www.cnblogs.com/FranZhou/p/5622638.html
Copyright © 2011-2022 走看看