iOS正则表达式验证邮箱,电话,手机号

//验证邮箱是否合法

+ (BOOL)validateEmail:(NSString *)emailString

{

    /**

     * :suneerwqe@sina.com

     */

    NSString *regexp = @"[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+\\.[a-zA-Z]{2,4}";

    

    NSPredicate *validate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp];

    

    return [validate evaluateWithObject:emailString];

}

 

//验证电话号码是否合法

+ (BOOL)validateTelephoneNumber:(NSString *)telNum

{

    /**

     * 大陆地区固话及小灵通

     * 区号:010,020,021,022,023,024,025,027,028,029

     * 号码:七位或八位

     * :021-23450623

     */

    NSString *regexp_tel = @"^0(10|2[0-5789]|\\d{3})-?\\d{7,8}$";

    

    /**

     * 中国联通

     * 130,131,132,152,155,156,185,186

     */

    NSString *regexp_chinaunicom = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";

    

    /**

     * 中国电信

     * 133,153,180,189

     */

    NSString *regexp_chinatelecom = @"^1(33|53|8[09])\\d{8}$";

    

    /**

     * 中国移动

     * 135,136,137,138,139,150,151,157,158,159,182,187,188

     */

    NSString *regexp_chinamobile = @"^1(3[5-9]|5[017-9]|8[278])\\d{8}$";

    

    NSPredicate *validate_tel = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp_tel];

    NSPredicate *validate_chinaunicom = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp_chinaunicom];

    NSPredicate *validate_chinatelecom = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp_chinatelecom];

    NSPredicate *validate_chinamobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp_chinamobile];

    

    return

    [validate_tel evaluateWithObject:telNum] ||

    [validate_chinaunicom evaluateWithObject:telNum] ||

    [validate_chinatelecom evaluateWithObject:telNum] ||

    [validate_chinamobile evaluateWithObject:telNum];

}

posted @ 2017-02-10 16:01  嗷大张  阅读(401)  评论(0编辑  收藏  举报