2

in my iphone app users can enter a specific ip-address/URL to connect to their own server and i'm trying to check the validity of the entered address. So if a user entered something like "1234" the app would throw an error because it does not "look" like an ip-address or a URL. Right now i'm trying to accomplish this by using the following method:

- (BOOL)urlIsValid:(NSString *)address {

NSString *urlRegEx = @"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:address];
}

And it I thought it was working just fine but now apparently even if I enter a correct ip-address the method will return "NO". Any ideas on what could be wrong? Thanks in advance!

Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Chris
  • 391
  • 2
  • 6
  • 17

3 Answers3

1

Read the Apple Docs
Especially the Listing 1, where the code says if (theConnection)

Lelouch Lamperouge
  • 8,171
  • 8
  • 49
  • 60
  • That didn't really provide any help, because it does not provide any documentation on how to check if the entered ip address conforms to a certain standart... – Chris Mar 17 '11 at 23:05
  • I'm sorry. I didn't quite get what you just said. What do you mean by **certain standard** of IP address. Can you give an example showing what you consider valid and what is invalid.
    I merely assumed that you wanted to check the existence of a server
    – Lelouch Lamperouge Mar 17 '11 at 23:15
  • Well, theoretically a user could enter anything as an ip-address in the provided textfield. So a user could enter something completely random like: "bla" or "$$". In that case I need a method which checks if the entered text is actually a potential candidate for an ip-address and after that I can check if there's actually a server behind the address. I hope thats better understandable. – Chris Mar 17 '11 at 23:34
  • Yes, but i already know how to check for the existence of a server i just don't know how to check for the validity of the ip address – Chris Mar 17 '11 at 23:43
  • Effectively, you want to check 2 things.
    1) Check format of the _entered_ ip address. [Are you the same Chris?][1]
    2) Existence of the server. This is already answered

    [1]: http://stackoverflow.com/questions/5154545/iphone-sdk-how-to-check-if-ip-entered-by-user-is-valid
    – Lelouch Lamperouge Mar 17 '11 at 23:45
  • Yes I am the same chris.. And the reason i am asking this again is because the answer from the other question doesn't seem to work... – Chris Mar 17 '11 at 23:47
  • One rule: Regular Expressions! – Lelouch Lamperouge Mar 17 '11 at 23:50
0

I'm validating ip address with this way, try to add http(s) prefix so, it should be work.

NSString *urlRegEx = @"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
NSPredicate *ipTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [ipTest evaluateWithObject:address];

Good luck

fyasar
  • 3,996
  • 2
  • 42
  • 55
0

Using NSPredicate for this is overkill, and isn't really what it was intended for. You should use NSRegularExpression for doing a regex match. However, to fully validate an IP address with a single regular expression would be cumbersome, to say the least.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498