Here is the code I'm using, it validates 99% of the urls I give it, but it fails when there is a ":port" after an IP address in the URL.
-(BOOL) validateUrl: (NSString *) candidate {
//NSString *urlRegEx=@"(http|https)://(((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+)|(";
NSString *urlRegEx = @"^(http|https|ftp)\://(([a-zA-Z0-9-.]+\.[a-zA-Z]{2,3})|([0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d\.[0-2]*\d*\d))(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9-._\?\,\'/\+&%\$#\=~])*[^.\,)(\s]$";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", urlRegEx];
return [urlTest evaluateWithObject:candidate];
}
thanks for the help!
Why not use NSURL
? Try creating a URL with URLWithString:
and if it returns nil, your URL was malformed. If you don’t get nil in return, then you can check the host, port, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With