xcode 如何对文本字段进行验证?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7950555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do validation on textfield?
提问by Nilesh
I am making a registration form, in which I have 8 text fields and one submit button.
我正在制作一个注册表单,其中有 8 个文本字段和一个提交按钮。
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.
请给我一些建议,谢谢。
回答by Marc Brannan
Simple logic, in your Submit action check that your textField.text is not nil or empty (@""). If not textField.text show UIAlert. Like this
简单的逻辑,在您的提交操作中检查您的 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];
}
回答by Suraj Mirajkar
-(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
}
}
回答by Gopesh Gupta
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
将其放入所有文本字段的 if 语句中。如果条件为 true ,则显示警报视图
回答by Mat
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:
您必须先检查所有字段,有几种方法可以做到这一点。您可以控制每个字段的文本长度,并在某些字段为空时显示警报。您可以检查length
NSString的属性,因此您也可以对长度进行操作(例如密码必须为8 个字符,否则为Alert。)。如果您标记了这 8 个文本字段(或者如果您知道视图中只有这些文本字段),那么一个好方法可能是这样的:
for(UITextField * tf in [self.view subviews]){
if(![tf.text length]>0){
//show the alert
}
}
hope this helps.
希望这可以帮助。
回答by Mark
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
处理空文本字段。它当然适用于空文本字段,但如果其中有一些空格则失败。SO中的一些人建议在使用上面的代码进行评估之前先修剪字符串。见链接
回答by Mubin Mall
set tag property of all your text fields uniquely and access them using if(textFieldName.tag==*yourtag*)
in button action event...
唯一地设置所有文本字段的标记属性并使用if(textFieldName.tag==*yourtag*)
按钮操作事件访问它们...