判斷輸入的手機號和價格是否合法

來源:互聯網
上載者:User

標籤:

  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(0, 0, 20, 20)];  
  58.     textfield1_.delegate = self;  
  59.     [self addSubview:textfield1_];  
  60.   
  61.      textfield2_ = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];  
  62.     textfield2_.delegate = self;  
  63.     [self addSubview:textfield2_];  
  64.   
  65.      textfield3_ = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];  
  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;  

判斷輸入的手機號和價格是否合法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.