码迷,mamicode.com
首页 > 其他好文 > 详细

No.2 电话,邮箱....各类文本框条件判断

时间:2016-04-28 14:20:42      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

使用:

1.倒入#import "HlZzPd.h"

2.找到对应的方法 例:

  邮箱判断调用:BOOL res =  [HlZzPd HlvalidateEmail:@"240146334@qq.com"];

  if(res){

    NSLog(@"邮箱合法");

  }else{

    NSLog(@"邮箱不合法");

  }

//
//  HlZzPd.h
//  DeliverGoods
//
//  Created by  黄磊 on 16/4/26.
//  Copyright © 2016年  黄磊. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface HlZzPd : NSObject

//判断邮箱是否合法
+ (BOOL) HlvalidateEmail:(NSString *)email;

//判断手机号码是否合法
+ (BOOL) HlvalidateMobile:(NSString *)mobile;

//生份证验证
+ (BOOL) HlvalidateIdentityCard: (NSString *)identityCard;

//密码验证
+ (BOOL) HlvalidatePassword:(NSString *)passWord;

//车牌号验证
+ (BOOL) HlvalidateCarNo:(NSString *)carNo;

//用户名
+ (BOOL) HlvalidateUserName:(NSString *)name;

@end

  

//
//  HlZzPd.m
//  DeliverGoods
//
//  Created by  黄磊 on 16/4/26.
//  Copyright © 2016年  黄磊. All rights reserved.
//

#import "HlZzPd.h"

@implementation HlZzPd

//邮箱
+ (BOOL) HlvalidateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}

//手机号码验证
+ (BOOL) HlvalidateMobile:(NSString *)mobile
{
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}

//身份证号
+ (BOOL) HlvalidateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}

//密码
+ (BOOL) HlvalidatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}

//车牌号验证
+ (BOOL) HlvalidateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    NSLog(@"carTest is %@",carTest);
    return [carTest evaluateWithObject:carNo];
}

//用户名
+ (BOOL) HlvalidateUserName:(NSString *)name
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
}

@end

  

No.2 电话,邮箱....各类文本框条件判断

标签:

原文地址:http://www.cnblogs.com/huanglert/p/5442312.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!