Tagged: iphone RSS Toggle Comment Threads | Keyboard Shortcuts

  • Sujith Krishnan 10:24 am on March 3, 2009 Permalink | Reply
    Tags: email, email syntax, iphone, NSPredicate, validation   

    Email syntax validation -iPhone Programming 

    Understanding that iPhone SDK dont support NSPredicate which is there for MAC Apps SDK i wrote an Utility Method to do a syntax validation of email.

    -(BOOL)validateEmail:(NSString*)email
    
    if( (0 != [email rangeOfString:@"@"].length) &&  (0 != [email rangeOfString:@"."].length) )
    	{
    		NSMutableCharacterSet *invalidCharSet = [[[[NSCharacterSet alphanumericCharacterSet] invertedSet]mutableCopy]autorelease];
    		[invalidCharSet removeCharactersInString:@"_-"];
    
    		NSRange range1 = [email rangeOfString:@"@" options:NSCaseInsensitiveSearch];
    
    		// If username part contains any character other than "."  "_" "-"
    
    		NSString *usernamePart = [email substringToIndex:range1.location];
    		NSArray *stringsArray1 = [usernamePart componentsSeparatedByString:@"."];
    		for (NSString *string in stringsArray1) {
    			NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet: invalidCharSet];
    			if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
    				return NO;
    		}
    
    		NSString *domainPart = [email substringFromIndex:range1.location+1];
    		NSArray *stringsArray2 = [domainPart componentsSeparatedByString:@"."];
    
    		for (NSString *string in stringsArray2) {
    			NSRange rangeOfInavlidChars=[string rangeOfCharacterFromSet:invalidCharSet];
    			if(rangeOfInavlidChars.length !=0 || [string isEqualToString:@""])
    				return NO;
    		}
    
    		return YES;
    	}
    	else // no '@' or '.' present
    		return NO;
    
    

     
    • Sujith Krishnan 6:11 pm on March 23, 2009 Permalink | Reply

      Hi all,

      This is an update.
      The iPhone SDK 3.0 (beta) is having NSPredicate API…
      The job is now simplified..
      No need to write your custom email syntax validator.
      Now we can use the regular expression…

      Still i love my validator method… :)

    • Jathin 9:52 am on August 12, 2010 Permalink | Reply

      Loved your code. I love it much more than the NSPredicate API…Good job…

    • dileep 11:26 am on December 8, 2010 Permalink | Reply

      hi sukri..
      great validator… it did help alot…

      1. NSString *emailRegex = @”[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}”;
      2. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];

      will return add.add@..com as a valid one..
      but urs works great..

      • Sujith Krishnan 1:49 pm on May 21, 2011 Permalink | Reply

        @dileep

        well.. the Predicate validator is again by some developer and not by apple i believe.

        happy to know that you are preferring my code over the most popular. :)

  • Sujith Krishnan 5:52 am on February 6, 2009 Permalink | Reply
    Tags: c, , conditional compilation, iphone, ipod, objective c   

    Conditional compilation – easy way !! 

    I achieved this by adding a #define in .pch file for each project.

    and do #import based on that definition using #ifdef

    This is the pretty cool and easy way  if your are sharing single source code set across multiple target/build/project

    Just make use of different .pch file for different builds.

    You want to create <n>  number of .pch file for <n> targets. Assign corresponding  .pch file for each target’s info


     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel
Follow

Get every new post delivered to your Inbox.