-1

I want display "1 (xxx)-(xxx)-(xxx)" in a UITextfield and only enter 10 digits.

  • It's valid 1 to 9 numbers.
  • It's starting only 1.
  • If you enter 0 it's not taken.

How do I achieve this?

Phanindra
  • 57
  • 1
  • 10

1 Answers1

2

I have done Us Phone Number validation in one of my projects. Hope this helps you.

-(void)textFieldDidBeginEditing:(UITextField *)textField {
 [textField performSelector:@selector(shouldChangeTextInRange:replacementText:)];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
 NSString *text = textField.text;

    NSString *acceptedcharacters = @"0123456789-/";
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:acceptedcharacters] invertedSet];
    const char * _char = [string cStringUsingEncoding:NSUTF8StringEncoding];
    int isBackSpace = strcmp(_char, "\b");
    if (isBackSpace == -8) {
        NSLog(@"deleted");
    }
    else {
        if (textField.text.length == 1) {

            textField.text = [NSString stringWithFormat:@"%@-",text];
            return YES;
        }
        if (textField.text.length == 5) {

            textField.text = [NSString stringWithFormat:@"%@-",text];
            return YES;
        }
        if (textField.text.length == 9) {

            textField.text = [NSString stringWithFormat:@"%@-",text];
            return YES;
        }
    }
    //   if (textField == self.phoneNumber_txtField) {
    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 14) ? NO : YES;

    //  }

    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];

    return [string isEqualToString:filtered];
 }
Shruti
  • 1,849
  • 1
  • 13
  • 21
  • how to call this method. – Phanindra Mar 16 '15 at 07:00
  • This is the delegate of `UITextField`. You set your texfield delegate as self. And place this method in your ViewController. It will be automatically called – Shruti Mar 16 '15 at 07:02
  • If you want to call it explicitly then.... `-(void)textFieldDidBeginEditing:(UITextField *)textField { [textField performSelector:@selector(shouldChangeTextInRange:replacementText:)];` } – Shruti Mar 16 '15 at 07:03
  • i was already set delegate method but it doesn't call. – Phanindra Mar 16 '15 at 07:05
  • you can call this explicitly in `-(void)textFieldDidBeginEditing:(UITextField *)textField` method using the perform selector. Like i mentioned above – Shruti Mar 16 '15 at 07:31
  • I wouldn't call this explicitly I'd figure out why its not working with the delegate instead. So @phanindramandarapu have you set the delegate for the `UITextField` llike `setDelegate:self`? And have you added the `UITextFieldDelegate` to your interface file (`.h`)? – Popeye Mar 16 '15 at 08:59
  • '-[UITextField shouldChangeTextInRange:replacementText:]: unrecognized selector sent to instance 0x8f1b740'. this code is crash. i was set delegate metthod. – Phanindra Mar 16 '15 at 09:19
  • its not allow zero's in UITextfield – Phanindra Mar 18 '15 at 09:22