Validate URL on iPhone - iphone

I was trying to validate URL links using regex, but not all the links are being completely identified. Can you please help me out?
I want the links to follow the pattern:
http://www.abcdef.org/xyz/content.aspx?menu id=190&id=3214

Assuming you are looking for a regular expression to match urls with a specific pattern:
You can use something like this to match http://www.abcdef.org/xyz/content.aspx?menu id=190&id=3214:
http://.*?/[a-zA-z]+/content.aspx\?menu id=\d+?&id=\d+

Regex for url with port in Objective c
It works well if don't have port number.
-(BOOL) validateUrl: (NSString *) candidate {
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];
}

Related

Multiple string variables in a function ios

I'm really new to ios and i'm trying to write a function that recieves 4 string,
Heres what I try:
this is in my h file:
-(BOOL)validate:(NSString*)fullname:(NSString*)email: NSString username:(NSString*)password
this is in my m file:
-(BOOL)validate:(NSString*)fullname:(NSString*)email: NSString username:(NSString*)password
{
NSString *emailRegex = #"[A-Z0-9A-z._%+- ]+#[A-Za-z0-0.-]+\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#",emailRegex];
return YES;
}
the errors is get are that iam using an undeclared identifier
on the emailRegex
and on the emaitTest
Basicly all i'm trying to do is declare a method that recieves 4 strings:
fullname
email
username
password
and implement it.
any help would be good.
good day.
It should be as :
-(BOOL)validateFullName:(NSString*)fullName email:(NSString*)email userName:(NSString*)username withPassword:(NSString*)password;
For Better readability :
-(BOOL)validateFullName:(NSString*)fullName
email:(NSString*)email
userName:(NSString*)username
password:(NSString*)password;
*Edit: Naming convention was not best, so renamed.

Regular expression for url in iPhone

I am using a regular expression to validate a website address. But it is not working because its format is not correct in iPhone. Its logic is almost correct. Can anyone rebuild this regular expression for me in iPhone ?
(?<http>(http:[/][/]|www.)([a-z]|[A-Z]|[0-9]|[/.]|[~])*)
Check out this one
NSString *urlRegEx =
#"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
use this bellow method
- (BOOL) validateUrl: (NSString *) stringURL {
NSString *urlRegEx =
#"(http|https)://((\\w)*|([0-9]*)|([-|_])*)+([\\.|/]((\\w)*|([0-9]*)|([-|_])*))+";
NSPredicate *urlTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", urlRegEx];
return [urlTest evaluateWithObject:stringURL];
}
hope this help you...
:)
For anyone having problem with accepted answer regexpression here is a version I'm using:
(https?)://(([\\w\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)\\-\\+\\=\\(\\)\\[\\]\\{\\}\\?\\<\\>])*)+([\\.|/](([\\w\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)\\-\\+\\=\\(\\)\\[\\]\\{\\}\\?\\<\\>])*))+
The problem with accepted answer regexp is that:
\w - already contains "_" and numbers
it fails for URLs having "-" after first dot
This will catch http / https / plain subdomain.domain.suffix links:
(Based on the good answer by Grzegorz Krukowski)
#"((https?)://)?(([\\w\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)\\-\\+\\=\\(\\)\\[\\]\\{\\}\\?\\<\\>])*)+([\\.|/](([\\w\\!\\#\\#\\$\\%\\^\\&\\*\\(\\)\\-\\+\\=\\(\\)\\[\\]\\{\\}\\?\\<\\>])+))+"
In my case, this is the definitive expression:
NSString *URLRegEx =
#"((https?://www[.][a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].{2,}[.][a-zA-Z0-9]{2,})|(www[.][a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].{2,}[.][a-zA-Z0-9]{2,})|([a-zA-Z0-9\-]{2,}[.][a-zA-Z0-9]{2,}))";
Example cases in which it matches:
http://www.example.com
http://www.example.es
https://www.example.com
www.example.com
example.com
Cases where it does not match:
example
http://example
http://example.com
www.example
etc...
If you don't want to allow all these cases, you just need to remove them from the expression.

Parsing URL Using Regular Expression

I need to parse a URL in the following format:
http://www.example.com/?method=example.method&firstKey=firstValue&id=1893736&thirdKey=thirdValue
All I need is the value of 1893736 within &id=1893736.
I need to do the parsing in Objective-C for my iPhone project. I understand it must have something to do with regular expression. But I just have no clue how to do it.
Any suggestions would be appreciated. :)
You don't need a regex for this. You can try something like this
NSString *url = #"http://www.example.com/?method=example.method&firstKey=firstValue&id=1893736&thirdKey=thirdValue";
NSString *identifier = nil;
for (NSString *arg in [[[url pathComponents] lastObject] componentsSeparatedByString:#"&"]) {
if ([arg hasPrefix:#"id="]) {
identifier = [arg stringByReplacingOccurrencesOfString:#"id=" withString:#""];
}
}
NSLog(#"%#", identifier);
Don't use regular expressions. Use NSURL to reliably extract the query string and then use this answer's code to parse the query string.
Use this:
.*/\?(?:\w*=[^&]*&)*?(?:id=([^&]*))(?:&\w*=[^&]*)*
And grap first group: \1. You will obtain 1893736.
Simplifying
If the id can consist of only digits:
.*/\?(?:\w*=[^&]*&)*?(?:id=(\d*))(?:&\w*=[^&]*)*
If you don't care about capturing uninterested groups (use \3 or id in this case):
.*/\?(\w*=.*?&)*?(id=(?<id>\d*))(&\w*=.*)*
More simpler version (use \3):
.*/\?(.*?=.*?&)*(id=(\d*))(&.*?=.*)*
Instead of using regex, you can split the string representation of your NSURL instance. In your case, you can split the string by the appersand (&), loop the array looking for the prefix (id=), and get the substring from the index 2 (which is where the = ends).

Regular expression to allow only string and number

I'm making the pattern to check the user can only enter number and string. Please anyone can help me or give some reference. Following is my code. Thnks in advance.
NSString *regEx =
#"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
#"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
#"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")#(?:(?:[a-z0-9](?:[a-"
#"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
#"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
#"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
#"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";
NSString *strRoadworthyGet=#"vinod123";
NSPredicate *matchPattern =[NSPredicate predicateWithFormat:#"SELF MATCHES %#",regEx];
BOOL resultMatch =[matchPattern evaluateWithObject:strRoadworthyGet];
if(!resultMatch)
NSLog(#"invalid pattern");
You can use NSCharacterSet's alphanumeric method to return check for Letters, Marks and Numbers.
If you want to go for RegularExpression, you can use
NSString *regEx = #"^[a-zA-Z0-9]*$";
Defining legal input characters

iPhone Objective-C Form Validation

I am an old developer but new to Objective-C and iPhone development. I am looking for the best way to do form validation for values entered. I have been googling for a while and can't find any good code, but might be using the wrong key worrds, etc... I am looking for things like catching for empty string, numeric validation, date validation, etc... where it catches this either while the user is entering data (input mask type) or on the lost focus of the control.
If you can point me to some good resources that would be great!
Thanks!
Simon.
I don't know particular resources but you could begin usin NSPredicate to validate fields with regular expressions. For example, for mail validation
BOOL result;
NSString* emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate* emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
result = [emailTest evaluateWithObject:anEmailAddress];
The emailRegex suggested by Espuz has a slight problem. The range operator "-" within the [] brackets should be escaped otherwise emails having "-" character are returned as invalid.
I am using the following regex string for pattern matching that allows "_", "-" and "." characters in the address:
NSString *emailRegex = #"[a-zA-Z0-9.\\-_]{2,32}#[a-zA-Z0-9.\\-_]{2,32}\.[A-Za-z]{2,4}";
NSPredicate *regExPredicate =
[NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
BOOL validEmail = [regExPredicate evaluateWithObject:txtField.text];