Alter reg-ex for email Validation on iphone - iphone

I am trying to validate email using reg-ex. here is the code...
+ (BOOL) stringIsValidEmail:(NSString *)checkString;
{
NSString *emailRegEx =
#"(?:[a-zA-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])+)\\])";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegEx];
return [emailTest evaluateWithObject:checkString];
}
now I don't have much knowledge about regex but this accepts a#a.c as a valid email. But this should not be the case and at least two characters should be required at the end. What paramater do I need to change in this so it returns false. I have hit and tried but that didn't work. Thanks for your help.

Too much symbols, you can try this
- (BOOL) IsValidEmail:(NSString *)checkString {
BOOL sticterFilter = YES;
NSString *stricterFilterString = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSString *laxString = #".+#.+\\.[A-Za-z]{2}[A-Za-z]*";
NSString *emailRegex = sticterFilter ? stricterFilterString : laxString;
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:checkString];
}

Try using this as regex
NSString *emailRegex = #"[A-Z0-9a-z._]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
the {2,4} especially validates that the ending characters should be alphabets and more than two in count

Related

Objective regex evaluateWithObject is not working

When I tried matching the string with the regex '^(34|37)' it does not work even after giving the correct one. Can anyone please point out or guide me to what I am doing wrong?
This is my code:
NSPredicate *myTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", #"^(34|37)"];
if([myTest evaluateWithObject: #"378282246310005"]){
NSLog(#"match");
}
Your regex will not match the given string. That is ^(34|37) does not match 378282246310005. It matches the first two characters, but after that it fails because the string contains more characters, while your regex terminates.
You need to alter your regex to match the rest of the characters, even if you don't want to capture them. Try changing your regext to ^(34|37).*.
Make seprate method for matching regex as bool type. Then it will work.
like this
- (IBAction)tapValidatePhone:(id)sender
{
if(![self validateMobileNo:self.txtPhoneNo.text] )
{
NSLog(#"Mobile No. is not valid");
}
}
-(BOOL) validateMobileNo:(NSString *) paramMobleNo
{
NSString *phoneNoRegex = #"^(34|37)";
NSPredicate *phoneNoTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#",phoneNoRegex];
return [phoneNoTest evaluateWithObject:#"3435"];
}
it is not going in else condition.
Why not just use hasPrefix:
if([#"378282246310005" hasPrefix:#"34"] || [#"378282246310005" hasPrefix:#"37"])
{
NSLog(#"found it");
}
EDIT:
Using NSPredicate:
NSPredicate *myTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", #"^3(4|7)\\d+$"];
if([myTest evaluateWithObject: #"378282246310005"])
{
NSLog(#"match");
}
else
{
NSLog(#"notmatch");
}
Using NSRegularExpression:
NSError *error = nil;
NSString *testStr = #"348282246310005";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:#"^3(4|7)" options:NSRegularExpressionCaseInsensitive error:&error];
NSInteger matches = [regex numberOfMatchesInString:testStr options:NSMatchingReportCompletion range:NSMakeRange(0, [testStr length])];
if(matches > 0 )//[myTest evaluateWithObject: #"378282246310005"])
{
NSLog(#"match");
}
else
{
NSLog(#"notmatch");
}
BTW: (34|37) does not look 34 or 37 instead it seems 347 or 337 to me, since engine will pick 4|3 either 4 or 3.

NSPredicate not working

I have this string:
<td align="right"><span> 19:45 </span></td>
I want to use a NSPredicate on it to search for the 19:45 part but every possible combination I tried returns nothing! I'm kinda losing my marbles here so please help!
Things i've tried:
NSString *timeStringPredicate = #"[0-9]:[0-9]";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:#"SELF like %#", timeStringPredicate];
if ([timeSearch evaluateWithObject:dayText]) {
NSLog(#"This is a time");
}
Or in these possibilities:
NSString *timeStringPredicate = #"[0-9]\\:[0-9]";
NSString *timeStringPredicate = #"*[0-9]:[0-9]*";
NSString *timeStringPredicate = #"*[0-9]\\:[0-9]*";
NSString *timeStringPredicate = #"*.[0-9]:[0-9].*";
NSString *timeStringPredicate = #"*.[0-9]\\:[0-9].*";
And about everything else.
Help!
like doesn't use regexp syntax. For that, you need to use matches instead. See The Predicate Programming Guide for details.
NSString *timeStringPredicate = #".*\\:[0-9].*";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:#"SELF matches %#", timeStringPredicate];
Try to do this:
NSString *timeStringPredicate = #".*\\:[0-9].*";
NSPredicate *timeSearch = [NSPredicate predicateWithFormat:#"SELF matches '%#'", timeStringPredicate];

NSPredicate use in iOS

Can anyone knows how to use NSPredicate for below format?
[Any letter][Any Number][Any letter][space][Any Number][Any letter][Any Number]
I want to validate string fot above fromat.
Thanks.
Use this format.
NSString *str1 = #"a8D 9k3";
NSString *str2 = #"a8 9k3";
NSString *testFormat = #"[a-zA-z][0-9][a-zA-z] [0-9][a-zA-z][0-9]";
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF matches %#", testFormat];
Following is valid:
BOOL isValid = [predicate evaluateWithObject:str1];
Following is invalid:
BOOL isValid = [predicate evaluateWithObject:str2];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"SELF MATCHES[c] %#", #"[a-z][0-9][a-z] [0-9][a-z][0-9]"];
if ([pred evaluateWithObject:#"a3B 5C9"])
{
NSLog(#"It matches!");
}

Checking if a string is a number on iPhone

How can I check if an input string is a number like x.y?
Try this code,
NSString *nameRegex =#"[0-9]+\\.[0-9]$";
NSPredicate *nameTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", nameRegex];
BOOL isDecimalNumber=[nameTest evaluateWithObject:string];
There is one more solution.
NSString *str = #"123456";
NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet];
BOOL valid = [[str stringByTrimmingCharactersInSet: decimalSet] isEqualToString:#""];

Email Validation iPhone SDK

Assuming I have created IBOutlet UITextField *emailValidate;
And the empty method
-(IBAction)checkEmail:(id)sender {
// Add email validation code here.
}
And linked the File Owner file to the TextField, what code would I have to insert in the method to validate an email adress? checking that only one '#' is included, and only one '.' is included?
Use the function below...
+(BOOL) validateEmail: (NSString *) email
{
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
BOOL isValid = [emailTest evaluateWithObject:email];
return isValid;
}
In my case I use a regex found at this blogpost:
NSString *emailRegEx =
#"(?:[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])+)\\])";
You can determine if there is exactly one "#" by splitting the string on '#' and checking for 2 pieces.
int numberOfAtPieces = [[emailValidate.text componentsSeparatedByString:#"#"] count];
if ( numberOfAtPicess != 2 ) { // show error alert }
else { // let it through }
You can get set of code from the following link . Hope this may helpful
I've used the solution shared by Macarse (the big regexp) for a few weeks with success, but I suddenly ran into a problematic case. It does not pass the test with "test1_iPhone#neywen.net" for instance.
So I chose to go back to the simpler solution provided by S P Varma (the small and simple regexp).
You could call the following method on the text of the UITextField:
- (BOOL)validateEmail:(NSString *)candidate {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:candidate];
}
Please adapt the emailRegex regular expression to your needs.