0

I am using the normal regular expression which most of us are using for Email validation.

Problem : Suppose if my Email id is : a@a.com, then after "com" if I press space twice, a full - stop gets appeared automatically.

I am using following code for validating email.

- (BOOL)validateEmailWithString:(NSString*)email
{
    NSLog(@"helloo in validate");
    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:email];
}

How do I avoid this full-stop ????

As I am a newbie to iphone development, Please help me out !!

NSException
  • 1,268
  • 3
  • 14
  • 28

2 Answers2

0
-(BOOL)validateEmail:(NSString *)inputText {
NSString *emailRegex = @"[A-Z0-9a-z][A-Z0-9a-z._%+-]*@[A-Za-z0-9][A-Za-z0-9.-]*\\.[A-Za-z]{2,6}"; 
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; 
NSRange aRange;
if([emailTest evaluateWithObject:inputText]) 
{
    aRange = [inputText rangeOfString:@"." options:NSBackwardsSearch range:NSMakeRange(0, [inputText length])];
    int indexOfDot = aRange.location;
    NSLog(@"aRange.location:%d - %d",aRange.location, indexOfDot);
    if(aRange.location != NSNotFound) 
    {
        NSString *topLevelDomain = [inputText substringFromIndex:indexOfDot];
        topLevelDomain = [topLevelDomain lowercaseString];
        NSLog(@"topleveldomains:%@",topLevelDomain);
        NSSet *TLD;
        TLD = [NSSet setWithObjects:@".aero", @".asia", @".biz", @".cat", @".com", @".coop", @".edu", @".gov", @".info", @".int", @".jobs", @".mil", @".mobi", @".museum", @".name", @".net", @".org", @".pro", @".tel", @".travel", @".ac", @".ad", @".ae", @".af", @".ag", @".ai", @".al", @".am", @".an", @".ao", @".aq", @".ar", @".as", @".at", @".au", @".aw", @".ax", @".az", @".ba", @".bb", @".bd", @".be", @".bf", @".bg", @".bh", @".bi", @".bj", @".bm", @".bn", @".bo", @".br", @".bs", @".bt", @".bv", @".bw", @".by", @".bz", @".ca", @".cc", @".cd", @".cf", @".cg", @".ch", @".ci", @".ck", @".cl", @".cm", @".cn", @".co", @".cr", @".cu", @".cv", @".cx", @".cy", @".cz", @".de", @".dj", @".dk", @".dm", @".do", @".dz", @".ec", @".ee", @".eg", @".er", @".es", @".et", @".eu", @".fi", @".fj", @".fk", @".fm", @".fo", @".fr", @".ga", @".gb", @".gd", @".ge", @".gf", @".gg", @".gh", @".gi", @".gl", @".gm", @".gn", @".gp", @".gq", @".gr", @".gs", @".gt", @".gu", @".gw", @".gy", @".hk", @".hm", @".hn", @".hr", @".ht", @".hu", @".id", @".ie", @" No", @".il", @".im", @".in", @".io", @".iq", @".ir", @".is", @".it", @".je", @".jm", @".jo", @".jp", @".ke", @".kg", @".kh", @".ki", @".km", @".kn", @".kp", @".kr", @".kw", @".ky", @".kz", @".la", @".lb", @".lc", @".li", @".lk", @".lr", @".ls", @".lt", @".lu", @".lv", @".ly", @".ma", @".mc", @".md", @".me", @".mg", @".mh", @".mk", @".ml", @".mm", @".mn", @".mo", @".mp", @".mq", @".mr", @".ms", @".mt", @".mu", @".mv", @".mw", @".mx", @".my", @".mz", @".na", @".nc", @".ne", @".nf", @".ng", @".ni", @".nl", @".no", @".np", @".nr", @".nu", @".nz", @".om", @".pa", @".pe", @".pf", @".pg", @".ph", @".pk", @".pl", @".pm", @".pn", @".pr", @".ps", @".pt", @".pw", @".py", @".qa", @".re", @".ro", @".rs", @".ru", @".rw", @".sa", @".sb", @".sc", @".sd", @".se", @".sg", @".sh", @".si", @".sj", @".sk", @".sl", @".sm", @".sn", @".so", @".sr", @".st", @".su", @".sv", @".sy", @".sz", @".tc", @".td", @".tf", @".tg", @".th", @".tj", @".tk", @".tl", @".tm", @".tn", @".to", @".tp", @".tr", @".tt", @".tv", @".tw", @".tz", @".ua", @".ug", @".uk", @".us", @".uy", @".uz", @".va", @".vc", @".ve", @".vg", @".vi", @".vn", @".vu", @".wf", @".ws", @".ye", @".yt", @".za", @".zm", @".zw", nil];
        if(topLevelDomain != nil && ([TLD containsObject:topLevelDomain])) 
        {
            //NSLog(@"TLD contains topLevelDomain:%@",topLevelDomain);
            return TRUE;
        }
//            else {
//             NSLog(@"TLD DOEST NOT contains topLevelDomain:%@",topLevelDomain);
//             }

    }
}
return FALSE;
}

den chek

if ([self validateEmail:txtFieldUserName.text]) 
{
    NSLog(@"done");
} 
else
{
 NSLog(@"Not done");
}

maybe it will help you... thanx...

ChintaN -Maddy- Ramani
  • 5,156
  • 1
  • 27
  • 48
0

A full stop appearing after two spaces is normal behavior. Most users know this and won't be surprised when it happens. You can turn it off in the settings (General->Keyboard->"." Shortcut).

If you really want to prevent it from happening, you can use the code in this answer. Note that you'll have to set up the text field delegate for this to be called.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)text
{
    //Check if a space follows a space
    if ( (range.location > 0 && [text length] > 0 &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[text characterAtIndex:0]] &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]]) )
    {
        //Manually replace the space with your own space, programmatically
        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@" "];

        //Make sure you update the text caret to reflect the programmatic change to the text view
//      textField.selectedTextRange = NSMakeRange(range.location+1, 0);  

        //Tell Cocoa not to insert its space, because you've just inserted your own
        return NO;
    }
    return YES;
}
Community
  • 1
  • 1
nevan king
  • 112,709
  • 45
  • 203
  • 241