有時(shí)候往往需要我們?nèi)ヅ袛嚯娫捥柎a輸入的格式是否正確
下面一個(gè)簡單的方法就可以解決這個(gè)問題:
#pragma mark 判斷電話號是否正確
- (BOOL)checkTel:(NSString *)str
{
if ([str length] == 0) {
[self tiShiWithTitle:@"電話號不能為空"];//提示用戶信息
return NO;
}
NSString *regex = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0-8])\\d{8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:str];
if (!isMatch) {
[self tiShiWithTitle:@"請輸入正確的手機(jī)號"];//提示用戶信息
return NO;
}
return YES;}
方法可以直接用,返回值是一個(gè)BOOL類型的浮禾,根據(jù)BOOL類型來判斷電話號碼格式交胚,如下使用:
if ( [self checkTel:phoneTextfield.text] ){
//電話號碼正確
}