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!