美文网首页
校验字符串

校验字符串

作者: PZcoder | 来源:发表于2016-11-04 15:38 被阅读62次

    目录:
    1、校验字符串是否为空
    2、判断字符串是否符合邮政编码格式
    3、判断字符串是否全是整数
    4、判断字符串是否全部是字符
    5、判断用户名是否符合要求
    6、判断密码是否符合要求
    7、判断电话号码是否合法
    8、判定string第一位是否从A-Z
    9、验证数字及位数
    10、验证字母及位数
    11、验证数字或字母
    12、验证邮箱格式是否正确

    1、是否为空,返回 YES|NO

    +(BOOL)isEmptyString:(NSString*)string
    {
        if (string == nil) 
        {
            // 如果字符串为空,返回YES
            return YES;
        }
    
        //去空格之后判断length是否为0(此判断方法也可用于判断其他字符)
        NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *content = [string stringByTrimmingCharactersInSet:whitespace];
    
        if ([content length] == 0)
        {
             return YES;//length为0,返回YES
        }
        
        return NO;//其余情况返回NO
    }
    

    2、判断字符串是否符合邮政编码格式

    + (BOOL)checkZipCode:(NSString *)coder
    {
        if (coder.length != 6)
        {
             return NO;//不是6位,不符合。
        }
        
        NSError *error = NULL;
        NSString *ruleString = @"[0-9]\\d{5}(?!\\d)";//邮政编码规则
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:ruleString
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:&error];
        NSUInteger numberOfMatches = [regex numberOfMatchesInString:coder
                                                            options:0
                                                              range:NSMakeRange(0, [coder length])];
    
        if(numberOfMatches == 1)
        {
            return YES;//numberOfMatches为1时符合规则。
        }
        else
        {
            return NO;
        }
    }
    

    3、判断输入字符串是否全部是整数

    + (BOOL)isPureInt:(NSString *)string
    {
        NSScanner *scan = [NSScanner scannerWithString:string];
    
        int value;
      
        BOOL scanIntVal = [scan scanInt:&value] ;
        BOOL scanAtEnd = [scan isAtEnd];
        BOOL intValue = [string intValue] > 0;
    
        //满足以上三个条件则是整数
        return (intVal && scanAtEnd && intValue);
    }
    

    4、判断输入是否全部都是字符(不允许有汉字、数字等,不能为空)

    + (BOOL)checkAllIsLetter:(NSString *)psw
    {
        BOOL hasOther = NO;
        int length = [psw length];
    
        if (length == 0) 
        {
            return NO;//输入字符串为空
        }
        
        //判断是否有汉字
        for (int i = 0; i < length; i++) 
        {
            NSRange range = NSMakeRange(i, 1);
            NSString *subString = [psw substringWithRange:range];
            const char *cString = [subString UTF8String];
            
            if (strlen(cString) == 3)
            {
                return NO;//有汉字
            }
        }
        
        //判断是否含有数字
        const char *s = [psw UTF8String];//转化成C语言字符串
    
        for (int i = 0; i < length; i++)
        {
            if (s[i] >= '0' && s[i] <= '9')
            {
                hasOther = YES;//含有数字
            }
        }
    
        return !hasOther;
    }
    

    5、判断用户名是否符合要求( 1:小于30位。2:数字+字母。3:不能包含其它字符)

    + (BOOL)checkUserName:(NSString *)psw
    {
        BOOL hasOther = NO;
        int length = [psw length];
        
        //判断是否是小于30位
        if(length > 30)
        {
            return NO;
        }
        
        //判断是否有汉字
        for (int i = 0; i < length; i++)
        {
            NSRange range = NSMakeRange(i, 1);
            NSString *subString = [psw substringWithRange:range];
            const char *cString = [subString UTF8String];
    
            if  (strlen(cString) == 3)
                return NO;
        }
    
        //判断是否是数字+字母
        const char *s=[psw UTF8String];
        for(int i = 0;i < length; i++)
        {
            if (s[i] >= '0' && s[i] <= '9')
                continue;
            else if (s[i] >= 'a' && s[i] <= 'z')
                continue;
            else if (s[i] >= 'A' && s[i] <= 'Z')
                continue;
            else
                hasOther = YES;
        }
        
        if(hasOther)
            return NO;
        else
            return YES;
    }
    

    6、判断密码是否符合要求(1:8-16位置。2:数字+字母。3:不能全是数字或全是字母)

    + (BOOL)checkPsw:(NSString *)psw
    {
        BOOL hasNum = NO;
        BOOL hasLetter = NO;
        BOOL hasOther = NO;
        
        int length = [psw length];
        
        //判断是否是8-16位
        if (length > 16 || length < 8)
            return NO;
        
        //判断是否有汉字
        for (int i = 0; i < length; i++)
        {
            NSRange range = NSMakeRange(i, 1);
            NSString *subString = [psw substringWithRange:range];
            const char *cString = [subString UTF8String];
    
            if (strlen(cString) == 3)
                return NO;
        }
        
        //判断是否是数字+字母
        const char *s=[psw UTF8String];
        for (int i = 0;i < length; i++)
        {
            if (s[i] >= '0' && s[i] <= '9')
                hasNum = YES;
            else if (s[i] >= 'a' && s[i] <= 'z')
                hasLetter = YES;
            else if (s[i] >= 'A' && s[i] <= 'Z')
                hasLetter = YES;
            else
                hasOther = YES;
        }
        
        if (hasNum && hasLetter && !hasOther)
            return YES;
        else
            return NO;
    }
    

    7、判断电话号码是否合法

    + (BOOL)isMobileNumber:(NSString *)mobileNum
    {
        /**
         * 手机号码
         * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         * 联通:130,131,132,152,155,156,185,186
         * 电信:133,1349,153,180,189
         */
        //NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
        /**
         10         * 中国移动:China Mobile
         11         * 134[0-8],135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,17**
         12         */
        NSString *CM = @"^1(34[0-8]|(3[5-9]|5[0127-9]|8[23478]|7[0-9])\\d)\\d{7}$";
        /**
         15         * 中国联通:China Unicom
         16         * 130,131,132,155,156,185,186,17**
         17         */
        //modify by zhangcz, 添加联通4G,176的号,而且没有152的号
        NSString *CU = @"^1(3[0-2]|5[56]|8[56]|7[6])\\d{8}$";
        /**
         20         * 中国电信:China Telecom
         21         * 133,1349,153,180,189,17**
         22         */
        NSString *CT = @"^1((33|53|8[09]|7[0-9])[0-9]|349)\\d{7}$";
        
        //NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
        NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
        NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
        NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
        
        if (([regextestcm evaluateWithObject:mobileNum] == YES)
            || ([regextestct evaluateWithObject:mobileNum] == YES)
            || ([regextestcu evaluateWithObject:mobileNum] == YES))
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    

    8、判定string第一位是否是从A-Z

    /*
     *判定string第一位是否是从A-Z
     */
    + (BOOL)isAlphabet:(NSString *)firstAlphabet
    {
        NSString *IDENTITYNUM= @"^[A-Z]$";
        NSPredicate *regextestBankNum = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",IDENTITYNUM];
    
        if ([regextestBankNum evaluateWithObject:firstAlphabet] == YES)
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    

    9、验证数字及位数(numStr:需要验证的字符串,count:需要验证的位数)

    + (BOOL)isNumber:(NSString*)numStr count:(int)count
    {
        NSString * regex = @"^[0-9]*$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
        if ([pred evaluateWithObject:numStr] && numStr.length == count) 
        {
            return YES;
        }
    
        return NO;
    }
    

    10、验证字母及位数

    +(BOOL)isLetter:(NSString *)letterStr count:(int)count
    {
        NSString *regex = @"^[A-Za-z]*$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
        if ([pred evaluateWithObject:letterStr] && letterStr.length == count) 
        {
            return YES;
        }
    
        return NO;
    }
    

    11、验证是数字或字母

    +(BOOL)isLetterOrNum:(NSString*)str count:(int)count
    {
        NSString * regex = @"^[A-Za-z0-9]*$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
        if ([pred evaluateWithObject:str] && str.length == count)
        {
            return YES;
        }
    
        return NO;
    }
    

    12、判断邮箱格式是否正确

    + (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];
    }
    

    相关文章

      网友评论

          本文标题:校验字符串

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