gpt4 book ai didi

ios - 电子邮件验证不适用于 textField shouldChangeCharactersInRange

转载 作者:行者123 更新时间:2023-11-28 22:03:05 26 4
gpt4 key购买 nike

您好,我想在用户在文本字段中输入文本时验证电子邮件地址。当错误的电子邮件然后显示警报并返回否以留在电子邮件文本字段中时,它正在处理返回和文本字段结束编辑但不处理文本字段 shouldChangeCharactersInRange。如果有人知道如何做,请分享它,请提前致谢。这是我用于验证的示例代码作为返回

- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeTextfield=textField;
if(activeTextfield==txtCon1Email || activeTextfield==txtCon1SecondMail || activeTextfield==txtCon2Email || activeTextfield==txtCon2SecondMail)
{
NSString *emailReg = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailReg];

if ([emailTest evaluateWithObject:textField.text] == NO)
{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OTS" message:@"Please enter valid email address." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];



isvalidemail=NO;


}
else
isvalidemail=YES;

}
}

但我想在用户输入文本时执行此操作

最佳答案

对于您的情况,这可能就足够了:只需尝试使用 textFieldDidEndEditing 委托(delegate):

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if ([textField.text length]==0 || [self validateEmailWithString:textField.text]) {
[textField resignFirstResponder];

}else{
[[[UIAlertView alloc] initWithTitle:@"OTS" message:@"Please enter valid email address." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]show];

[textField becomeFirstResponder];
return NO;
}
return YES;
}

- (BOOL)validateEmailWithString:(NSString*)email
{
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];
}

关于ios - 电子邮件验证不适用于 textField shouldChangeCharactersInRange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24646312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com