美文网首页
iOS正则表达式验证用户名、邮箱等信息是否合法

iOS正则表达式验证用户名、邮箱等信息是否合法

作者: 凯文Kevin21 | 来源:发表于2017-11-29 11:05 被阅读30次
    • 判断字符串是否有空格
    +(BOOL)isHaveEmptyString:(NSString *)str{
        NSRange range = [str rangeOfString:@" "];
        if (range.location != NSNotFound) {
            return YES;
        }
        else {
            return NO;
        }
    }
    
    • 判断IP地址是否正确
    +(BOOL)isValidateIpAddress:(NSString *)ip{
        NSString *emailRegexp = @"(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|[1-9])\\.(25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d{2}|\\d)\\.(25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d{2}|\\d)\\.(25[0-5]|2[0-4]\\d|1\\d{1,2}|\\d{2}|\\d)";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailRegexp];
        return [emailTest evaluateWithObject:ip];
    }
    
    • 判断密码格式是否正确
    +(BOOL)isValidateUserpassword:(NSString *)password{
        
        NSString *passwordRegexp = @"[\\d*[a-z]*[A-Z]*[{\\[(<~!@#$%^&*()_+=-`|\"?,./;'\\>)\\]}]*]*";
        NSPredicate *PasswordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",passwordRegexp];
        return [PasswordTest evaluateWithObject:password];
    }
    
    • 判断用户名格式是否正确(目前校验规则是大小写字符+数字+'@'特殊符号, 其他特殊符号全部过滤掉)
    +(BOOL)isValidateUsername:(NSString *)name{
        NSString *userRegexp =  @"^[A-Za-z0-9@]{2,18}+$";
        NSPredicate *userTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userRegexp];
        return [userTest evaluateWithObject:name];
    }
    
    • 判断邮箱格式是否正确
    +(BOOL)isValidateEmail:(NSString *)email{
        NSString *userRegexp = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";   //邮箱格式
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userRegexp];
        return [emailTest evaluateWithObject:email];
        }
    }
    
    • 隐藏某个控制器的状态栏
    - (BOOL)prefersStatusBarHidden {
        returnYES;
    }
    

    相关文章

      网友评论

          本文标题:iOS正则表达式验证用户名、邮箱等信息是否合法

          本文链接:https://www.haomeiwen.com/subject/flnebxtx.html