验证手机号

来源:互联网 发布:河图二手专辑淘宝店 编辑:程序博客网 时间:2024/03/29 18:10
验证手机号:
- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.        tf =[[UITextField alloc]initWithFrame:CGRectMake(20, 20, 200, 30)];    tf.layer.borderColor = [[UIColor grayColor]CGColor];    tf.layer.borderWidth = 2;    tf.delegate = self;    [tf addTarget:self action:@selector(inputing) forControlEvents:UIControlEventEditingChanged];    [self.view addSubview:tf];        btn = [UIButton buttonWithType:UIButtonTypeCustom];    btn.frame = CGRectMake(40, 60, 50, 50);    btn.enabled = FALSE;    [btn setTitle:@"yanzheng" forState:UIControlStateNormal];//    btn.backgroundColor = [UIColor redColor];    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];//    [btn setTitleColor:[UIColor redColor] forState:UIControlEventTouchUpInside];    [btn addTarget:self action:@selector(dianji) forControlEvents:UIControlEventTouchUpInside];    [self.view addSubview:btn];}- (void)inputing{    NSLog(@"%@",tf.text);    if (tf.text.length ==11) {        BOOL isphone = [self isMobileNumber:tf.text];    if (isphone) {        btn.enabled = YES;    }else{        btn.enabled = FALSE;    }    }else{        btn.enabled =  FALSE;    }}- (void)dianji{    }- (void)textFieldDidBeginEditing:(UITextField *)textField{    NSLog(@"kaishi");}- (BOOL)isMobileNumber:(NSString *)mobileNum{    if ([mobileNum length] == 0) {                UIAlertView* alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"提示", nil) message:NSLocalizedString(@"手机号码不能为空", nil) delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [alert show];//        [alert release];        return NO;    }    //1[0-9]{10}    //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$    //    NSString *regex = @"[0-9]{11}";    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];    BOOL isMatch = [pred evaluateWithObject:mobileNum];    if (!isMatch) {        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [alert show];//        [alert release];        return NO;    }    return YES;}

0 0