iOS開發正則表達式 判斷手機號碼,郵箱格式是否正確

//正則表達式,判斷手機號碼格式是否正確

- (BOOL)checkTel:(NSString *)str

{

    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:str];

    if (!isMatch) {

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請輸入正確的手機號碼" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

        

        [alert show];

        return NO;

    }

    return YES;

}


//正則判斷郵箱格式是否正確

- (BOOL)validateEmail:(NSString *)email

{

    NSLog(@"3333333");

    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

    BOOL isMatch = [emailTest evaluateWithObject:email];

    if (!isMatch) {

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"請輸入正確的郵箱" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

        

        [alert show];

        return NO;

    }

    return YES;

}

發佈了30 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章