Search code examples
iosobjective-cuitextfieldphone-numbermasking

Add hyphen automatically in text field but not able to edit the textfield (Phone number masking)


Using the below code, I am able to add hyphen automatically, but not able to do editing in the textfield properly.

For example when I click to insert any number between the already entered numbers in the textfield, this will insert the number at the end of the textfield and further not delete that number properly. Can anyone help me out?

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  {    
     if(textField == txtUserName)
     {
         if (range.location == 12)
         {
             return NO;
         }
         if (range.length == 0 && ![[NSCharacterSet decimalDigitCharacterSet]   characterIsMember:[string characterAtIndex:0]])
         {
             return NO;
         }       
        if (range.length == 0 && (range.location == 3 || range.location == 7))
         {
             txtUserName.text = [NSString stringWithFormat:@"%@-  %@",txtUserName.text,string];
             return NO;
         }
         if (range.length == 1 &&(range.location==4 ||range.location ==7))      
         {
             range.location--;
             range.length = 2;
              txtUserName.text = [txtUserName.text  stringByReplacingCharactersInRange:range withString:@""];
             NSLog(@"Nisha..%@",txtUserName.text);
             return NO;
          }
    }   
    return YES;
}

Solution

  • This code will also work when we will do copy-paste text in UITextField.

    Warning- This code won't work with your keyboard's delete button. Anyway you need only keyboard's backspace button because Mobile keypad's delete button work like keyboard's backspace button

    Note- use tag for your UITextField instead of directly compare textField to your outlet

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        //(textField == txtUserName)
        // don't compare with ID, use tag
        if(textField.tag == 0 ) 
        {
            NSString *text = textField.text;
            NSUInteger textLength = text.length;
            NSString *trimText = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
            NSUInteger trimTextLength = trimText.length;
    
            if (range.length == 0 && [string rangeOfCharacterFromSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound)
            {
                return NO;
            }
            if( string.length > 0 ) {
                NSString *replacedText = text;
                if(trimTextLength < 10) {
                    NSInteger remainingNumbers = (10-trimTextLength);
                    if(string.length > remainingNumbers) {
                        string = [string substringToIndex:remainingNumbers];
                    }
                    replacedText = [replacedText stringByReplacingCharactersInRange:range withString:string];
                }
                NSString *trimReplacedText = [replacedText stringByReplacingOccurrencesOfString:@"-" withString:@""];
                if( trimReplacedText.length > 3 ) {
                    trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(3, 0) withString:@"-"];
                }
                if( trimReplacedText.length > 7 ) {
                    trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(7, 0) withString:@"-"];
                }
                textField.text = trimReplacedText;
                return NO;
            }
            bool flag = false;
            if (range.length == 1 &&(range.location == 4 || (range.location == 7 && (textLength-trimTextLength)== 1 && [text rangeOfString:@"-"].location == 7) || (range.location == 8 && (textLength-trimTextLength)== 2) ))
            {
                range.location--;
                range.length = 2;
                flag = true;
            }
            else if (range.length >= 1)
            {
                flag = true;
            }
    
            if(flag) {
                NSString *replacedText = [textField.text stringByReplacingCharactersInRange:range withString:@""];
    
                NSString *trimReplacedText = [replacedText stringByReplacingOccurrencesOfString:@"-" withString:@""];
                if( trimReplacedText.length > 3 ) {
                    trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(3, 0) withString:@"-"];
                }
                if( trimReplacedText.length > 7 ) {
                    trimReplacedText = [trimReplacedText stringByReplacingCharactersInRange:NSMakeRange(7, 0) withString:@"-"];
                }
                textField.text = trimReplacedText;
                return NO;
            }
        }
        return YES;
    }