2016-01-05 16:21:26 sevenquan 阅读数 1517


//通过区分字符串

-(BOOL)validateEmail:(NSString*)email

{

    if((0 != [email rangeOfString:@"@"].length) &&

       (0 != [email rangeOfString:@"."].length))

    {

        NSCharacterSet* tmpInvalidCharSet = [[NSCharacterSet alphanumericCharacterSet] invertedSet];

        NSMutableCharacterSet* tmpInvalidMutableCharSet = [[tmpInvalidCharSet mutableCopy] autorelease];

        [tmpInvalidMutableCharSet removeCharactersInString:@"_-"];

        

        

        NSRange range1 = [email rangeOfString:@"@"

                                      options:NSCaseInsensitiveSearch];

        

        //取得用户名部分

        NSString* userNameString = [email substringToIndex:range1.location];

        NSArray* userNameArray   = [userNameString componentsSeparatedByString:@"."];

        

        for(NSString* string in userNameArray)

        {

            NSRange rangeOfInavlidChars = [string rangeOfCharacterFromSet: tmpInvalidMutableCharSet];

            if(rangeOfInavlidChars.length != 0 || [string isEqualToString:@""])

                return NO;

        }

        

        //取得域名部分

        NSString *domainString = [email substringFromIndex:range1.location+1];

        NSArray *domainArray   = [domainString componentsSeparatedByString:@"."];

        

        for(NSString *string in domainArray)

        {

            NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:tmpInvalidMutableCharSet];

            if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])

                return NO;

        }

        

        return YES;

    }

    else {

        return NO;

    }

}




// 利用正则表达式验证

-(BOOL )isValidateEmail:( 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];

}

2016-11-15 13:58:20 IT_ZGC 阅读数 265

#pragma mark    ======判断邮箱的格式==========

-(BOOL)isValidateEmail:(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];

    

}


2016-05-19 16:43:00 y_csdnblog_xx 阅读数 182
//
//  TextDetection.m
//  RE
//
//  Created by YX-KuYu on 16/5/19.
//  Copyright © 2016年 YX-KuYu. All rights reserved.
//
// 验证   邮箱   密码   用户名

#import "TextDetection.h"


//邮箱的正则表达式
#define EMAILREG  @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"

//用户名的正则表达式   中英文、数字、下划线,至少2个字符,至多20
#define NAMEREG   @"^[\\w\\u4e00-\\u9fa5]{2,20}$"

//密码的正则表达式   以字母开头,长度在6~18之间,只包含英文、数字和下划线
#define PASSWORDREG @"^[a-zA-Z]\\w{5,17}$"

//预注册用户密码正则表达式  以M开头+身份证后6位数
#define PREREGISTERPASSWORDREG @"^[M]\\w{6}$"

//手机号的正则表达式
#define PHONEREG  @"^1[35678][\\d]{9}$"

//邮编的正则表达式
#define POSTCODEREG @"^[\\d]{6}$"

//图片格式验证
#define PICTURESTYLEREG @"/.+(\\.jpg|\\.jpeg|\\.gif|\\ .png)$/i"

//验证中文
#define CHINESEREG  @"[^\u4e00-\u9fa5]"

//字母和数字
#define CHARSNUMREG  @"^[0-9a-z]+$/i"

//社保卡
#define SOCIALCARDSREG  @"^[A-Za-z0-9]{0,30}$/i"

//身份证号
#define IDCARDREG  @"^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$"



@implementation TextDetection

/**
 *  用于验证输入框的内容
 *
 *  @param regText   需要检测的正则表达式
 *  @param candidate 需要检测的字符串
 *
 *  @return 返回的是否为真
 */
+ (BOOL)validate:(NSString *)regText withCandidate:(NSString *)candidate{
    
    NSPredicate *predicateText = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regText];
    
    return [predicateText evaluateWithObject:candidate];
    
}

//验证邮箱
+ (BOOL)validateEmail:(NSString *)candidate{
    
    return [self validate:EMAILREG withCandidate:candidate];
    
}

//验证姓名
+ (BOOL)validateName:(NSString *)candidate{
    
    return [self validate:NAMEREG withCandidate:candidate];
    
}

//手机号
+ (BOOL)validatePhone:(NSString *)candidate{
    
    return [self validate:PHONEREG withCandidate:candidate];
    
}

//密码
+ (BOOL)validatePassword:(NSString *)candidate{
    
    return [self validate:PASSWORDREG withCandidate:candidate];
    
}

//预注册密码
+ (BOOL)validatePrePassword:(NSString *)candidate{
    
    return [self validate:PREREGISTERPASSWORDREG withCandidate:candidate];
    
}

//邮编
+ (BOOL)validatePostCode:(NSString *)candidate{
    
    return [self validate:POSTCODEREG withCandidate:candidate];
    
}

//图片格式
+ (BOOL)validatePictureType:(NSString *)candidate{
    
    return [self validate:PICTURESTYLEREG withCandidate:candidate];
    
}

//中文
+ (BOOL)validateChinese:(NSString *)candidate{
    
    return [self validate:CHINESEREG withCandidate:candidate];
    
}

//字母和数字
+ (BOOL)validateCharNum:(NSString *)candidate{
    
    return [self validate:CHARSNUMREG withCandidate:candidate];
    
}

//社保卡号
+ (BOOL)validateSocialCard:(NSString *)candidate{
    
    return [self validate:SOCIALCARDSREG withCandidate:candidate];
    
}

//身份证号
+ (BOOL)validateIDCard:(NSString *)candidate{
    
    return [self validate:IDCARDREG withCandidate:candidate];
    
}


@end

2015-05-24 14:46:39 ljh910329 阅读数 477
// #pragma mark ---------------------------------------------- 判断邮箱格式
 + (BOOL)isValidateEmail:(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];
 }
// #pragma mark ---------------------------------------------- 判断手机号码
 + (BOOL)CheckPhoneNumInput:(NSString *)phone
 {
 NSString *Regex = @"(13[0-9]|14[57]|15[012356789]|18[012356789])\\d{8}";
 NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
 return [mobileTest evaluateWithObject:phone];
 }

2017-02-24 23:44:15 NoPolun_iOS 阅读数 2292
//进行邮箱正则表达式判断
-(BOOL) validateEmail
{
    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:self];

}


这里面的self 是因为这个方法是写在category的 NSString + SiZheString.m 中

所以调取方式应该是


if([@"字符串"  validateEmail]){

}

这种样式

如果是自己不想封装

  NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
   Bool  isTrue =  [emailTest evaluateWithObject:@"字符串"];

IOS 常用正则汇总

阅读数 345

IOS 常用正则汇总

博文 来自: yimiyuangguang
没有更多推荐了,返回首页