第一種使用正則表達式 判斷
//是否是純數(shù)字
+ (BOOL)isNumText:(NSString *)str{
NSString * regex = @"(/^[0-9]*$/)";
NSPredicate * pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:str];
if (isMatch) {
return YES;
}else{
return NO;
}
}
具體正則對不對 還需要大家來看以下
第二種 系統(tǒng)源生的
我推薦第二種
- (NSString *) trimming {
return [self stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
}
//判斷是不是純數(shù)字
[NSCharacterSet decimalDigitCharacterSet];
if ([[textField.text stringByTrimmingCharactersInSet: [NSCharacterSet decimalDigitCharacterSet]]trimming].length >0) {
DLog(@"不是純數(shù)字");
}else{
DLog(@"純數(shù)字!");
}
最近在做一個即時通訊的項目, 首先是注冊登錄界面, 項目需求是通過用戶輸入的手機號碼獲取一個4位數(shù)的驗證碼來完成注冊,那么, 問題來了?
如何判斷用戶輸入的手機號碼是合法的正確的11位手機號碼呢?(這些簡單的問題就在前端判斷好了再post給后臺 ,沒必要把各種沒用的數(shù)據(jù)都post給后臺)
判斷手機號碼是否正確的方法很多,我是用正則表達式來完成匹配的,廢話不多說,直接上代碼:
//正則表達式匹配11位手機號碼
NSString *regex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:_telField.text];
if(isMatch) { //有效手機號
}else//無效手機號
{
if (ios7) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"無效的手機號碼,請重新輸入..." delegate:self cancelButtonTitle:nil otherButtonTitles:@"確定", nil nil];
alertView.tag = 104;
[alertView show];
}else
{
UIAlertController*alertController = [UIAlertController alertControllerWithTitle:nil message:@"無效的手機號碼,請重新輸入..." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action) {
[_telField selectAll:self];
}];
[alertController addAction:otherAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}
}
聯(lián)通,移動和電信每年都會添加新的號碼,所以匹配電話號碼的正則表達式也要年年更新.
^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$
這個正則表達式我測試過了還沒發(fā)現(xiàn)有匹配不了的號碼,在這里分享給大家用!
您可能感興趣的文章:- iOS App開發(fā)中Objective-C使用正則表達式進行匹配的方法
- 正則表達式在IOS中的應用及IOS中三種正則表達式的使用與比較
- iOS 正則表達式判斷手機號碼、固話
- IOS開發(fā)常用的正則表達式
- iOS 中使用正則表達式判斷身份證格式及銀行卡號格式是否正確(推薦)
- iOS中使用正則表達式NSRegularExpression 來驗證textfiled輸入的內(nèi)容
- iOS正則表達式驗證手機號、郵箱、身份證號等
- ios利用正則表達式判斷手機號碼格式是否正確的實例
- Objective-C中利用正則去除非數(shù)字字母漢字方法實例