i have a text field in Contact screen and the user need to enter email address to send me message. Whats the best way to ensure the user has entered a valid email address su
Use this textField delegate function as this will be called on every text entered:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *strEnteredText = textField.text;
if(strEnteredText.length>0)
{
if([self validateEmail:strEnteredText])
{
//Valid email
//Use UILabel to give message
// BOOL email = true to know email is valid when submit button tapped
}
else
{
//Not Valid email
//Use UILabel to give message
// BOOl emaiL = false to know email is valid when submit button tapped
}
}
}
Add this method .h file
- (BOOL) validateEmail: (NSString *) enteredText
{
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:enteredText];
}