0

I have a UITextField where the user can enter in their phone number. I need the phone number to be in the following format: 5567289223 and not like this with an extra 1 at the beginning: 15567289223.

I already have an if statement setup so if the number is greater than 10 digits or less than 10 digits it will throw an error.

The only final problem I don't know how to solve is if the user tries putting an extra 1 in the beginning of the phone number.

I need to setup an if statement so that if the first character in the phone number is a "1" we will throw an error, or an even easier possible solution would be to use shouldChangeCharactersInRange so that if it's the first number they are typing in the UITextField then it cannot be a 1.

Someone gave me this solution earlier for controlling which characters are allowed to be typed into a UITextField: https://stackoverflow.com/a/22265084/3344977

I have been trying to modify it so that it will not allow the number "1" to be the first number typed into the UITextField but I cannot get it to work properly.

Any help is greatly appreciated.

Community
  • 1
  • 1
user3344977
  • 3,584
  • 4
  • 32
  • 88

3 Answers3

4

I have modified the example you want to use as per your need, you can check it out

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField==self.testTextField&&range.location==0)
{
    if ([string hasPrefix:@"1"])
    {
        return NO;
    }
}

return YES;
}
Omer Farooq
  • 3,754
  • 6
  • 31
  • 60
Harsh
  • 2,852
  • 1
  • 13
  • 27
  • This isn't working. I am able to type "1" and when I do and try to type the 2nd number the app crashes and gives me this error: Range or index out of bounds' – user3344977 Mar 08 '14 at 06:52
  • @MartinR I actually need to be able to enter the digit 1, just not as the very first digit being entered into the uitextfield. Anywhere after is fine. – user3344977 Mar 08 '14 at 06:52
  • i Have edited my post and also tested teh same.. working for me! :) – Harsh Mar 08 '14 at 07:02
  • This might fail if 1) you hit the "delete" key (because `string` is the empty string in that case and `characterAtIndex:0` will crash , or 2) you use copy/paste to replace the field contents (because `textField.text` is not empty in that case). – Martin R Mar 08 '14 at 07:09
  • @MartinR Fixing for that aswell, Thanks! And fixed one more bug if teh user manually goes to position 1 and changes the text replaced it teextfield.text.length with range.location! – Harsh Mar 08 '14 at 07:11
  • 1
    That's much better now! Note that you can simplify the character check to `if ([string hasPrefix:@"1"]) ...` . – Martin R Mar 08 '14 at 07:13
  • aaah.. That can be much more easier! :) Thanks a ton! – Harsh Mar 08 '14 at 07:15
0

See this answer,

UITextField text change event

i would setup an event handler like how it's mentioned in that example. and then an if statement inside the selector:

if([textField.text length] == 1 && [textField.text isEqualToString:@"1"]){

     //Do something.
}

Hope this helps

Community
  • 1
  • 1
Hakim
  • 1,242
  • 1
  • 10
  • 22
0

For Swift

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{

 if textField == self.txtFieldName && range.location == 0
    {
        if string.hasPrefix("1"){
            return false
        }
    }
    return true

}
Maulik Patel
  • 2,045
  • 17
  • 24