在iPhone App中如何在UITextField上添加电子邮件验证?
请帮助和建议
感谢。
NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:userMailTextField.text] == YES) {
}
else{
NSLog(@"email not in proper format");
}
答案 0 :(得分:0)
尝试使用此代码[ClassName validateEmail:txtEmail.text error:&alertMessage]
+ (BOOL) validateEmail: (NSString *) myEmail error:(NSString**)error
{
BOOL isValid = FALSE;
if ([Validations isValidData:myEmail])
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if (![emailTest evaluateWithObject:myEmail])
{
*error = @"Please enter vaild email address";
isValid = FALSE;
}
else
{
isValid = TRUE;
*error = @"";
}
}
else
{
*error = @"Please enter email address";
isValid = FALSE;
}
return isValid;
}
答案 1 :(得分:0)
只需检查UITextField就像txtemail
一样NSString *emailRegEx = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
//Valid email address
if ([emailTest evaluateWithObject:txtemail.text] == YES)
{
}
else
{
//email id is not proper
}