如何对文本字段进行验证?

发布于 2024-12-13 00:24:35 字数 138 浏览 0 评论 0原文

我正在制作一份注册表,其中有 8 个文本字段和一个提交按钮。

每当用户无法输入其中一个文本字段时,单击提交按钮后就会生成一条错误消息。

当用户填写所有文本字段时,单击提交按钮应该转到下一页。

请给我一些建议,谢谢。

I am making a registration form, in which I have 8 text fields and one submit button.

Whenever the user fails to enter one of the text fields, upon click of submit button an error message should be generated.

And when the user fills all the text fields, on click of submit button it should go to the next page.

Please give me some advice, thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

非要怀念 2024-12-20 00:24:35

简单的逻辑,在您的提交操作中检查您的 textField.text 不为零或空(@“”)。如果不是textField.text则显示UIAlert。像这样

-(IBAction) submitButton
{
  if(self.txtName == nil || [self.txtName.text isEqualToString:@""])
  {
    [self showErrorAlert];
  }

  if(self.txtEmail == nil || [self.txtEmail.text isEqualToString:@""])
  {
    [self showErrorAlert];
  }


}
// and show error alert as
-(void) showErrorAlert
{
    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"" 
                              message:@"All Fields are mandatory." delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil, nil];
    [ErrorAlert show];
    [ErrorAlert release];
}

Simple logic, in your Submit action check that your textField.text is not nil or empty (@""). If not textField.text show UIAlert. Like this

-(IBAction) submitButton
{
  if(self.txtName == nil || [self.txtName.text isEqualToString:@""])
  {
    [self showErrorAlert];
  }

  if(self.txtEmail == nil || [self.txtEmail.text isEqualToString:@""])
  {
    [self showErrorAlert];
  }


}
// and show error alert as
-(void) showErrorAlert
{
    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"" 
                              message:@"All Fields are mandatory." delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil, nil];
    [ErrorAlert show];
    [ErrorAlert release];
}
秋意浓 2024-12-20 00:24:35
 -(void)emptyTextfieldVaildation
{
  if( ([TxtFieldName.text isEqualToString:@""]) || ([TxtFieldPaswrd.text isEqualToString:@""]) )
  {

    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Error!!" 
                              message:@"Please fill in the details." delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil, nil];
    [ErrorAlert show];
    [ErrorAlert release];
  }
  else 
  {
    // Action to be called on Submit button touch
  }
}
 -(void)emptyTextfieldVaildation
{
  if( ([TxtFieldName.text isEqualToString:@""]) || ([TxtFieldPaswrd.text isEqualToString:@""]) )
  {

    UIAlertView *ErrorAlert = [[UIAlertView alloc] initWithTitle:@"Error!!" 
                              message:@"Please fill in the details." delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil, nil];
    [ErrorAlert show];
    [ErrorAlert release];
  }
  else 
  {
    // Action to be called on Submit button touch
  }
}
失与倦" 2024-12-20 00:24:35

单击提交按钮设置方法

-(IBAction)submit:(id)sender
{
   //Do all the textField Validation
}

通过

[textfield.text isEqualToString:@""]

将其放入所有文本字段的 if 语句中检查所有文本字段数据。如果条件为 true ,则显示警报视图

Set the method on click of submit button

-(IBAction)submit:(id)sender
{
   //Do all the textField Validation
}

Check the all textfield data by using

[textfield.text isEqualToString:@""]

put this into if statement for all text field.. If condition is true , then show alert View

骄兵必败 2024-12-20 00:24:35

您必须先检查所有字段,有多种方法可以做到这一点。您可以控制每个字段的文本长度,并在某些字段为空时显示警报。你可以检查NSString的length属性,这样你也可以对长度进行操作(例如密码必须是8个字符,否则Alert。)。
如果您已经标记了这 8 个文本字段(或者如果您知道您的视图中只有这个文本字段),一个好方法可能是这样的:

for(UITextField * tf in [self.view subviews]){
 if(![tf.text length]>0){
      //show the alert
  }
}

希望这会有所帮助。

You have to check all fields before, and there are several way to do this. You can control the lenght of text of each field and show an alert if some field is empty. You can check the length property of the NSString, so you can also operate on the length(e.g the password must be 8 char, otherwise Alert.).
If you have tagged these 8 textFields(or if you know that there are just this textfields in your view) a good way may be like this:

for(UITextField * tf in [self.view subviews]){
 if(![tf.text length]>0){
      //show the alert
  }
}

hope this helps.

比忠 2024-12-20 00:24:35

昨晚我也遇到了这个问题,我曾经

 if ([myTextField.text length] == 0) {
         // error code to handle empty text field. 
 }

处理空文本字段。当然,对于空文本字段,它可以工作,但如果其中有一些空格,则失败。 SO 中的一些人建议在使用上面的代码进行评估之前先修剪字符串。请参阅链接

如何在文本字段中启用 UIButton不为空?

Last night I struggled with this too and I used

 if ([myTextField.text length] == 0) {
         // error code to handle empty text field. 
 }

to handle empty text field. It works, for empty text field, of course, but failed if there are some spaces in there. Some people in SO give suggestion to trim the string first before evaluate using the code above. See link

How to enable a UIButton if a textfield is not empty?

那伤。 2024-12-20 00:24:35

唯一地设置所有文本字段的标签属性,并在按钮操作事件中使用 if(textFieldName.tag==*yourtag*) 访问它们...

set tag property of all your text fields uniquely and access them using if(textFieldName.tag==*yourtag*) in button action event...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文