Tuesday, November 6, 2012

UITextFiled Email Validation:




Check if the email address accepts only the following characters.
A) Alphabets : 'A to Z' or 'a to z'
b) Numeric : 0 to 9
c)Characters !#$%&'*+-/=?^_`{|}~
c.1. Character . (dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively (e.g. John..Doe@example.com).
c.2.Special characters are allowed with restrictions including:
Space and ""(),:;<>@[\]


Code Here:

@implementation EmailvalidationViewController
@synthesize txtEmail;
@synthesize btnMail;


- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad
{
[super viewDidLoad];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField == txtEmail)
{
NSCharacterSet *invalidCharSet = [[NSCharacterSet characterSetWithCharactersInString:@".ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@0123456789!#$%&'*+-/=?^_`{|}~"] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:invalidCharSet] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
static NSCharacterSet *charSet = nil;
if(!charSet) {
charSet = [[[NSCharacterSet characterSetWithCharactersInString:@"!#$%&'*+-/=?^_`{|}~"] invertedSet] retain];
}
NSRange location = [string rangeOfCharacterFromSet:charSet];
return (location.location == NSNotFound);
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[txtEmail resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"textFieldDidBeginEditing");
}


-(IBAction)btnMail_Clicked:(id)sender

{
NSLog(@"btnclicked");
BOOL boolValue = [self ValidateEmail:txtEmail.text];
}

-(BOOL)ValidateEmail:(NSString *)mailstring
{
NSString *mailstr = txtEmail.text;
NSRange openingRange = [mailstr rangeOfString:@".."];
if (openingRange.location!=NSNotFound)
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Mail Check" message:@"Pleasesfs Enter Correct Email" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil];
[alert show];
return NO;
}
if ([mailstr hasSuffix:@"."])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Mail Check" message:@"Please Enter Correct Email" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil];
[alert show];
return NO;
}
if ([mailstr hasPrefix:@"."])
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Mail Check" message:@"Please Enter Correct Email" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Cancel", nil];
[alert show];
return NO;
}
NSString *inputString = mailstr;
NSScanner *mainScanner = [NSScanner scannerWithString:inputString];
NSInteger numberOfChar=0;
while(![mainScanner isAtEnd])
{
[mainScanner scanUpToString:@"@" intoString:nil];
if(![mainScanner isAtEnd])
{
numberOfChar++;
[mainScanner scanString:@"@" intoString:nil ];
}
}
NSString *inputString1 = mailstr;
NSScanner *mainScanner1 = [NSScanner scannerWithString:inputString1];
NSInteger numberOfChar1=0;
while(![mainScanner1 isAtEnd])
{
[mainScanner1 scanUpToString:@"." intoString:nil];
if(![mainScanner1 isAtEnd])
{
numberOfChar1++;
[mainScanner1 scanString:@"." intoString:nil ];
}
}
if ((numberOfChar==1)&&(numberOfChar1 > 0))
{
NSLog(@"govings");
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""message:@"Please and check the email address."delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
return NO;
}
return YES;
}

@end


No comments:

Post a Comment