ios开发笔记--判断输入的手机号和价格是否合法

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  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.     NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];  
  35.     NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];  
  36.     NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];  
  37.     NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];  
  38.   
  39.     if (([regextestmobile evaluateWithObject:mobileNum] == YES)  
  40.         || ([regextestcm evaluateWithObject:mobileNum] == YES)  
  41.         || ([regextestct evaluateWithObject:mobileNum] == YES)  
  42.         || ([regextestcu evaluateWithObject:mobileNum] == YES))  
  43.         {  
  44.         return YES;  
  45.         }  
  46.     else  
  47.         {  
  48.         return NO;  
  49.         }  
  50. }  
  51.   
  52.  特殊字符的限制输入,价格金额的有效性判断  
  53.   
  54. #define myDotNumbers     @"0123456789.\n"  
  55. #define myNumbers          @"0123456789\n"  
  56. -(void) createTextFiled {  
  57.     textfield1_ = [[UITextField alloc] initWithFrame:CGRectMake(002020)];  
  58.     textfield1_.delegate = self;  
  59.     [self addSubview:textfield1_];  
  60.   
  61.      textfield2_ = [[UITextField alloc] initWithFrame:CGRectMake(002020)];  
  62.     textfield2_.delegate = self;  
  63.     [self addSubview:textfield2_];  
  64.   
  65.      textfield3_ = [[UITextField alloc] initWithFrame:CGRectMake(002020)];  
  66.     textfield3_.delegate = self;  
  67.     [self addSubview:textfield3_];  
  68.   
  69. }  
  70.   
  71. -(void)showMyMessage:(NSString*)aInfo {  
  72.   
  73.     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:aInfo delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil nil];  
  74.     [alertView show];  
  75.     [alertView release];  
  76.   
  77. }  
  78.   
  79. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {  
  80.     NSCharacterSet *cs;  
  81.     if ([textField isEqual:textfield1_]) {  
  82.         cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];  
  83.         NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  
  84.         BOOL basicTest = [string isEqualToString:filtered];  
  85.         if (!basicTest) {  
  86.             [self showMyMessage:@"只能输入数字"];  
  87.             return NO;  
  88.         }  
  89.     }  
  90.     else if ([textField isEqual:textfield2_]) {  
  91.         cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];  
  92.         NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  
  93.         BOOL basicTest = [string isEqualToString:filtered];  
  94.         if (!basicTest) {  
  95.             [self showMyMessage:@"只能输入数字"];  
  96.             return NO;  
  97.         }  
  98.     }  
  99.     else if ([textField isEqual:textfield3_]) {  
  100.         NSUInteger nDotLoc = [textField.text rangeOfString:@"."].location;  
  101.         if (NSNotFound == nDotLoc && 0 != range.location) {  
  102.             cs = [[NSCharacterSet characterSetWithCharactersInString:myDotNumbers] invertedSet];  
  103.         }  
  104.         else {  
  105.             cs = [[NSCharacterSet characterSetWithCharactersInString:myNumbers] invertedSet];  
  106.         }  
  107.         NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];  
  108.         BOOL basicTest = [string isEqualToString:filtered];  
  109.         if (!basicTest) {  
  110.   
  111.             [self showMyMessage:@"只能输入数字和小数点"];  
  112.             return NO;  
  113.         }  
  114.         if (NSNotFound != nDotLoc && range.location > nDotLoc + 3) {  
  115.             [self showMyMessage:@"小数点后最多三位"];  
  116.             return NO;  
  117.         }  
  118.     }  
  119.     return YES;  
  120. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值