iOS开发中手机号码和价格金额有效性判断及特殊字符的限制

来源:互联网 发布:船舶吨位是指 知乎 编辑:程序博客网 时间:2024/04/28 06:15

在实际开发过程中,经常会遇到些不能让用户随便地输入手机号码,对输入的手机号码的正确判断;有些输入框只能输入数字,不能输入字母或特殊字符;还有些如价格金额之类的就只能输入数字和小数点且小数点后面保留两位。

[html] view plaincopy
  1. // 手机号码的有效性判断  
  2.   
  3. - (BOOL)isMobileNumber:(NSString *)mobileNum  
  4. {  
  5.     /**  
  6.      * 手机号码  
  7.      * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188  
  8.      * 联通:130,131,132,152,155,156,185,186  
  9.      * 电信:133,1349,153,180,189  
  10.      */  
  11.     NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";  
  12.     /**  
  13.      10         * 中国移动:China Mobile  
  14.      11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188  
  15.      12         */  
  16.     NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";  
  17.     /**  
  18.      15         * 中国联通:China Unicom  
  19.      16         * 130,131,132,152,155,156,185,186  
  20.      17         */  
  21.     NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";  
  22.     /**  
  23.      20         * 中国电信:China Telecom  
  24.      21         * 133,1349,153,180,189  
  25.      22         */  
  26.     NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";  
  27.     /**  
  28.      25         * 大陆地区固话及小灵通  
  29.      26         * 区号:010,020,021,022,023,024,025,027,028,029  
  30.      27         * 号码:七位或八位  
  31.      28         */  
  32.     // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";  
  33.     /**  
  34.      29         * 国际长途中国区(+86)  
  35.      30         * 区号:+86  
  36.      31         * 号码:十一位  
  37.      32         */  
  38.      NSString * IPH = @"^\\+861(3|5|8)\\d{9}$";  
  39.       
  40.     NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];  
  41.     NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];  
  42.     NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];  
  43.     NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];  
  44.     NSPredicate *regextestiph = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", IPH];  
  45.     if (([regextestmobile evaluateWithObject:mobileNum] == YES)  
  46.         || ([regextestcm evaluateWithObject:mobileNum] == YES)  
  47.         || ([regextestct evaluateWithObject:mobileNum] == YES)  
  48.         || ([regextestcu evaluateWithObject:mobileNum] == YES)  
  49.         || ([regextestiph evaluateWithObject:mobileNum] == YES))  
  50.     {  
  51.         return YES;  
  52.     }  
  53.     else  
  54.     {  
  55.         return NO;  
  56.     }  
  57. }  

[html] view plaincopy
  1. //////// 特殊字符的限制输入,价格金额的有效性判断  
  2. #define myDotNumbers     @"0123456789.\n"  
  3. #define myNumbers        @"+0123456789\n"  
  4.   
  5. -(void) createTextFiled {  
  6.       
  7.     textfield1_ = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 110, 20)];  
  8.     textfield1_.delegate = self;  
  9.     textfield1_.text=@"";  
  10.     textfield1_.keyboardType=UIKeyboardTypePhonePad;  
  11.     textfield1_.textColor=[UIColor redColor];  
  12.     textfield1_.backgroundColor=[UIColor whiteColor];  
  13.     [self.view addSubview:textfield1_];  
  14.     [textfield1_ release];  
  15.       
  16.     textfield2_ = [[UITextField alloc] initWithFrame:CGRectMake(110, 0, 80, 20)];  
  17.     textfield2_.delegate = self;  
  18.     textfield2_.text=@"11";  
  19.     [self.view addSubview:textfield2_];  
  20.     [textfield2_ release];  
  21.       
  22.     textfield3_ = [[UITextField alloc] initWithFrame:CGRectMake(200, 0, 100, 20)];  
  23.     textfield3_.delegate = self;  
  24.     textfield3_.text=@"22";  
  25.     [self.view addSubview:textfield3_];  
  26.     [textfield3_ release];  
  27. }  
  28.   
  29. -(void)showMyMessage:(NSString*)aInfo {  
  30.       
  31.     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示"  
  32.                                                         message:aInfo  
  33.                                                        delegate:self  
  34.                                               cancelButtonTitle:@"确定"  
  35.                                               otherButtonTitles:nil, nil];  
  36.     [alertView show];  
  37.     [alertView release];  
  38. }  
  39.   
  40.   
  41. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {  
  42.     NSCharacterSet *cs;  
  43.     if ([textField isEqual:textfield1_]) {  
  44.         cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];  
  45.         NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  
  46.         BOOL basicTest = [string isEqualToString:filtered];  
  47.         if (!basicTest) {  
  48.             [self showMyMessage:@"只能输入数字"];  
  49.             return NO;  
  50.         }  
  51.     }  
  52.     else if ([textField isEqual:textfield2_]) {  
  53.         cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];  
  54.         NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  
  55.         BOOL basicTest = [string isEqualToString:filtered];  
  56.         if (!basicTest) {  
  57.             [self showMyMessage:@"只能输入数字"];  
  58.             return NO;  
  59.         }  
  60.     }  
  61.     else if ([textField isEqual:textfield3_]) {  
  62.         NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;  
  63.         if (NSNotFound == nDotLoc && 0 != range.location) {  
  64.             cs = [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers] invertedSet];  
  65.         }  
  66.         else {  
  67.             cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];  
  68.         }  
  69.         NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  
  70.         BOOL basicTest = [string isEqualToString:filtered];  
  71.         if (!basicTest) {  
  72.               
  73.             [self showMyMessage:@"只能输入数字和小数点"];  
  74.             return NO;  
  75.         }  
  76.         if (NSNotFound != nDotLoc && range.location > nDotLoc + 3) {  
  77.             [self showMyMessage:@"小数点后最多三位"];  
  78.             return NO;  
  79.         }  
  80.     }  
  81.     return YES;  
  82. }  
0 0