zoukankan      html  css  js  c++  java
  • 输入手机号 实现自动三 四 四格式

    用的是空格,如果用- 把空格换成- 就OK

    #define NUMBERS @"0123456789"

     1 //键盘改变,控制变更
     2 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     3     
     4     NSCharacterSet*cs;
     5     cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];
     6     NSString*filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
     7     BOOL basicTest = [string isEqualToString:filtered];
     8 
     9     NSInteger existedLength = textField.text.length;
    10     NSInteger selectedLength = range.length;
    11     NSInteger replaceLength = string.length;
    12     
    13     if (existedLength - selectedLength + replaceLength >14) {
    14         return NO;
    15     }
    16 
    17     
    18     if(basicTest)
    19     {
    20         if (textField == phoneTxtField)
    21         {
    22 
    23             if ([string isEqualToString:@""]){ // 删除字符
    24                 if (((textField.text.length - 2) % 4 == 0) &&((textField.text.length - 2) % 6 != 0)) {
    25                     textField.text = [textField.text substringToIndex:textField.text.length - 1];
    26                 }else if ((textField.text.length - 2) % 9 == 0){
    27                     textField.text = [textField.text substringToIndex:textField.text.length - 1];
    28                 }
    29                 return YES;
    30             } else {
    31                 if (textField.text.length<5) {
    32                     if (textField.text.length % 4 == 0) {
    33                         textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
    34                     }
    35                 }else{
    36                     if (textField.text.length % 9 == 0) {
    37                     textField.text = [NSString stringWithFormat:@"%@ ", textField.text];
    38                 }
    39                 }
    40                 
    41             }
    42             return YES;
    43         }
    44     }
    45     else
    46     {
    47         return NO;
    48     }
    49     
    50     
    51     if (string.length == 0) {
    52         
    53         return YES;
    54     }
    55     
    56     
    57     return YES;
    58     
    59 
    60     
    61 }
    62 
    63 
    64 //键盘关闭
    65 -(void)textFieldDidEndEditing:(UITextField *)textField
    66 {
    67     if ((textField == phoneTxtField) &&(textField.text.length==14) ){
    68        NSLog(@"输入了九位");

           

     userPhone=[phoneTxtField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
            NSLog(@"%@",userPhone);

    82

    83     }
    84 }
    让明天,不后悔今天的所作所为
  • 相关阅读:
    LeetCode 75. Sort Colors(按颜色进行排序)
    LeetCode 451. Sort Characters By Frequency(按照字符出现次数对字符串排序)
    LeetCode 347. Top K Frequent Elements(出现频率最多的 k 个元素)
    LeetCode 215. Kth Largest Element in an Array(数组求第k大)
    CF #629 Div.3 E(LCA)F
    系统函数
    CASE表达式
    循环得出数据库中所有的 DB_ID,DB_NAME
    数据库的编码问题
    检验临时表是否存在
  • 原文地址:https://www.cnblogs.com/-yun/p/5113821.html
Copyright © 2011-2022 走看看