textView判断格式,用户名,验证码,手机号

#pragma mark - 检测非法字符

/*手机号码验证 MODIFIED BY HELENSONG

 * 正确返回true

 */

-(BOOL) isValidateMobile:(NSString *)mobile

{

    //手机号以13, 15,18开头,八个 \d 数字字符

    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";

    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

    //    NSLog(@"phoneTest is %@",phoneTest);

    return [phoneTest evaluateWithObject:mobile];

}

登录点击按钮

- (void)button{

if (![self isValidateMobile:userName.text])

            {

                [MTHint toast:@"请输入正确手机号" toView:weakSelf.view displaytime:3];

                

                return;

            }

}

//姓名只能由中文、字母或数字组成

- (BOOL)isValidateUser:(NSString *)text

{

    NSString * regex = @"^[a-zA-Z0-9_\u4e00-\u9fa5]+$";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    

    return [pred evaluateWithObject:text];

}

 //验证码,由0到9,6位数

- (BOOL)isValidateNumber:(NSString *)text

{

    NSString * regex = @"^[0-9]{6}";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    

    return [pred evaluateWithObject:text];

}

在UIAlert方法里

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    

    XLOG(@"%d",buttonIndex);

    

    if (buttonIndex == 1)

    {

        //得到输入框

        UITextField *tf=[alertView textFieldAtIndex:0];

        

        if ([tf.text trimWhitespace].length <= 0)

        {

            [MTHint toast:@"不能为空" toView:self.view displaytime:2.0f];

            

            return;

        }

        

        if (![self isValidateUser:tf.text])

        {

            [MTHint toast:@"姓名只能由中文、字母或数字组成" toView:self.view displaytime:2.0f];

            

            return;

        }

        

        if (tf.text.length >= 10)

        {

            [MTHint toast:@"姓名需少于10个字符" toView:self.view displaytime:2];

            

            return;

        }

        

        [titleValues replaceObjectAtIndex:1 withObject:tf.text];

        nameValue = tf.text;

        XLOG(@"%@",tf.text);

        [_tableView reloadData];

    }

 

}

posted on 2014-12-05 15:07  rankilau  阅读(286)  评论(0编辑  收藏  举报

导航