1

我在一个视图中有很多文本字段,而不是用户填写所有详细信息并点击保存按钮,我只想验证在那里输入的电子邮件 ID。当用户转到下一个文本字段时如何在标签中给出错误消息

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:self.emailidTexField.text] == NO)
{
   // UILabel besides the text field and when the user goes to next field logic here.
}
4

7 回答 7

2

使用下面的代码..

在这里,当您单击 keyBoard 的 return 或 Next 键时,此波纹管textFieldShouldReturn: UITextField委托方法被调用,您可以编写任何具有以下条件的验证或代码...

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

    if (textField==self.emailidTexField) {
        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:self.emailidTexField.text] == NO)
        {
           // UILabel besides the text field and when the user goes to next field logic here.
           [self.emailidTexField resignFirstResponder];
           [YourNextTextFieldName becomeFirstResponder];
        }
        else{
           // Display message here that EmaiId is not valid
           return NO;
        }

    }
    return YES;
}

在这里,我设置了条件,如果textFieldself.emailidTexField ,则进入内部并检查验证是否为真,然后退出该 textField 键盘并将光标移至 NextTextField。

于 2013-08-02T06:15:58.953 回答
2
This may help... try this

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:emailidTexField])
     {
        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:self.emailidTexField.text] == NO)
             {
               [emailidTexField becomeFirstResponder];
             }
      else{
               [Yournexttextfield becomeFirstResponder];
           } 
      }
}
于 2013-08-02T07:42:00.907 回答
1

您可以将自己添加为文本字段的代表并在textFieldDidEndEditing:. 确保endEditing:在按下保存按钮时调用视图以确保委托方法运行。

于 2013-08-02T06:11:01.507 回答
1
 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:self.emailidTexField.text] == NO)
 {
    [your next field becomesfirstresponder];
 }

使您的下一个文本字段成为第一响应者以将控件移动到下一个字段。

于 2013-08-02T06:13:35.777 回答
1

当用户转到下一个时很简单UITextField。入住

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;

无论是否相同UITextField,您都可以设置标签UITextField's以查找。然后只需在UIAlertView其中添加一个:-

 - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
    {
          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:self.emailidTexField.text] == NO)
          {
                UIAlertView *_alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Email is Invalid !!!  " delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
          }
    }
于 2013-08-02T06:14:16.843 回答
1
 - (BOOL)textFieldShouldReturn:(UITextField *)textField {
      NSLog(@"%d",textFiled.tag);
      if(textField.tag==xxx){
    //check valid here
      }
      return YES;
  }
于 2013-08-02T06:09:20.243 回答
0

您必须设置文本字段的委托并在UITextField委托方法中编写以下代码

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if ([textField isEqual:emailidTexField])
     {
        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:self.emailidTexField.text] == NO)
             {
               [emailidTexField becomeFirstResponder];
             }
      else{
               [Yournexttextfield becomeFirstResponder];
           } 
      }
}
于 2013-08-02T06:13:26.810 回答