0

I have a TextField for IP Address. Currently I am validating my IP Address on click of a button like this:

- (IBAction)addPrinter:(id)sender {

     NSString* validIPRegEx = @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
     NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:validIPRegEx
                                  options:0
                                  error:nil];
     NSUInteger numberOfMatches = [regex numberOfMatchesInString:_printerIPAddress.text options:0 range:NSMakeRange(0, [_printerIPAddress.text length])];
    if (numberOfMatches!=1) {
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"Invalid IP Address"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles: nil];
        [myAlertView show];
    }
}

But, my requirement is that I validate my IP Address on the go as user is entering text in TextField.

For example when user start typing in the TextField he should be allowed to enter a number between 0-255 before any of the three dot in an IP Address i.e if user has already typed 26 then the text shouldn't change if he types anything except a dot. Similarly the text should not change if he types any other character except numbers and dot.

I have searched a lot about this but found no help. Any help regarding this will be appreciated.

Thanks in advance!

Exception
  • 2,273
  • 1
  • 24
  • 42
  • 1
    Well, try [`^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){0,3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])?$`](https://regex101.com/r/xQ2xZ2/1). – Wiktor Stribiżew Nov 26 '15 at 09:43
  • My issue is not with the regular expression, it is with how I can validate partially typed IP Address every time the user types something in TextField. – Exception Nov 26 '15 at 10:08
  • I know, did you try my regex for checking that? I should mention that you cannot use one and the same regex both for *live* and *final* validation. It is impossible. – Wiktor Stribiżew Nov 26 '15 at 10:09
  • @stribizhev, I tried your regex, but it is not giving me the desired result. For ex. it failed for string 100 which is a partially valid IP Address. – Exception Nov 26 '15 at 11:01
  • [It does not choke on `100`](https://regex101.com/r/xQ2xZ2/2). – Wiktor Stribiżew Nov 26 '15 at 11:04
  • Is my way of comparing string with regex correct? Because I checked once again it is giving `numberOfMatches` as 0 for string 100. – Exception Nov 26 '15 at 11:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96248/discussion-between-exception-and-stribizhev). – Exception Nov 26 '15 at 11:18
  • 1
    Oh, yes, I see. I guess you'd better use `NSPredicate` with `MATCHES`. See [this demo of your current regex](https://ideone.com/mV8EIN). You may insert my regex there and see how the behavior changes. – Wiktor Stribiżew Nov 26 '15 at 11:22
  • It will give yes for 100.100.100.100, but just for 100 it is giving no. – Exception Nov 26 '15 at 11:36
  • [It works alright](http://ideone.com/Az4ndM). – Wiktor Stribiżew Nov 26 '15 at 11:41
  • In the earlier demo you shared, the regex was wrong. I didn't realise that. Anyway it is working now. Thanks a lot! – Exception Nov 26 '15 at 11:56

2 Answers2

0
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
 ............
} 

Above this method will tell you when ever the user hits a key to change the text, a "paste" will cause multiple characters to be in the string, backspace over one, or delete of a selection will cause the string to be empty,

In this you can check the range of character 0-255.

Original answer is below.

Detecting whether the user has typed in a UITextField

Community
  • 1
  • 1
0

The complete answer is a combination of comments from stribizhev and Muhammad's answer. The above functionality can be implemented in the following way:

  1. Make your ViewController UITextFieldDelegate by adding _textfiled.delegate=self;
  2. Implement -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string in the following way:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
        NSString* ipEntered;
        if (![string isEqualToString:@""]) {
    
            ipEntered=[NSString stringWithFormat:@"%@%@",[textfield.text substringToIndex: range.location],string];
        }
    
        NSString* validIPRegEx = @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[.]){0,3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])?$";
        NSPredicate * emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", validIPRegEx];
        if ([string isEqualToString:@""]) {
    
            return YES;
        }
        else if ([emailTest evaluateWithObject:ipEntered]){
    
            return YES;
    
        }
        else{
    
            return NO;
        }
    
    
    }
    
Exception
  • 2,273
  • 1
  • 24
  • 42