主要用于判斷輸入到TextField的内容是不是數字,比如需要輸入電話号碼的時候。
網上檢視了一些資料,一般都是通過協定。
以下内容來自:http://www.2cto.com/kf/201404/291718.html
今天項目中有個文本框需要隻可以輸入數字的需求,首先在.xib中将UITextField的Keyboard設定為Number Pad,但是使用時鍵盤會切回别的鍵盤無法對内容進行校驗。通過神奇的百度我知道了通過以下方法可以解決這樣的問題:
首先讓.xib的viewController實作UITextFieldDelegate然後和想要校驗的控件關聯。
然後将下列代碼粘貼到類中即可。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return [self validateNumber:string];
}
- (BOOL)validateNumber:(NSString*)number {
BOOL res = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
int i = 0;
while (i < number.length) {
NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
if (range.length == 0) {
res = NO;
break;
}
i++;
}
return res;
}
還有一種方式,以下這種方式我還沒有試過但是我找到了就分享出來吧:
1.定義常量以備使用
#define NUMBERS @"0123456789"
2.進行以下操作
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
NSCharacterSet*cs;
cs = [[NSCharacterSetcharacterSetWithCharactersInString:NUMBERS] invertedSet];
NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
BOOLbasicTest = [string isEqualToString:filtered];
if(!basicTest) {
UIAlertView* alert = [[UIAlertViewalloc] initWithTitle:@"提示"
message:@"請輸入數字"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
returnNO;
}
returnYES;
}
後面的方法可以判斷任意字元串是不是純數字
參考:http://blog.csdn.net/sike2008/article/details/8315202
1用正規表達式
//是否是純數字
+ (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;
}
}
具體正則對不對 還需要大家來看以下 第二種 系統源生的 我推薦第二種
- (NSString *) trimming {
return [self stringByTrimmingCharactersInSet: [NSCharacterSetwhitespaceCharacterSet]];
}
//判斷是不是純數字
[NSCharacterSet decimalDigitCharacterSet];
if ([[textField.text stringByTrimmingCharactersInSet: [NSCharacterSetdecimalDigitCharacterSet]]trimming].length >0) {
DLog(@"不是純數字");
}else{
DLog(@"純數字!");
}
說說自己的方法
我是參考的最後一種方式,把判斷寫在一個分類裡面,友善要用的時候随時調用
經測試,完美可用
以下是分類中的代碼
@implementation NSString (Number)
+ (BOOL)isNumber:(NSString *)string{
//判斷是不是純數字
[NSCharacterSet decimalDigitCharacterSet];
if ([[string stringByTrimmingCharactersInSet: [NSCharacterSet decimalDigitCharacterSet]] trimming].length >0) {
return NO;
}else{
NSLog(@"純數字!");
}
return YES;
}
- (NSString *) trimming {
return [self stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
}
然後就是在用的時候,導入分類的頭檔案,調用方法就可以了
比如說:
if (![NSString isNumber:self.phoneTextField.text]) {
[self showAlertWithTittle:@"提示" andMessage:@"電話隻能是純數字" andOKTittle:@"知道了"];
}
彈出提示框因為用得比較多,是以把簡單的提示框單獨寫了一個方法
/**
* 顯示彈出提示框
*
* @param tittle 标題
* @param message 提示消息
* @param OKTittle 确定按鈕标題
*/
- (void)showAlertWithTittle:(NSString *)tittle andMessage:(NSString *)message andOKTittle:(NSString *)OKTittle{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:tittle message:message preferredStyle:UIAlertControllerStyleAlert];
// UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTittle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
// NSLog(@"點選了取消按鈕");
// }];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:OKTittle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
// [alertController addAction:cancelAction];
[alertController addAction:OKAction];
[self presentViewController:alertController animated:YES completion:nil];
}
其實,後面發現,寫在各個.m檔案中并不是很好,因為很多Controller都可能要用到這個方法,有興趣的朋友可以試試把這個方法封裝到一個類裡面。
轉載于:https://www.cnblogs.com/endtel/p/4871469.html