UI文本字段验证

发布于 2024-10-08 10:45:41 字数 112 浏览 1 评论 0原文

我正在尝试验证我的文本字段。我有两个文本字段用户名和密码。我想检查是否用户名文本字段保留为空,然后不允许用户移动到密码文本字段,直到他输入用户名。任何执行此操作的示例代码将不胜感激。

提前致谢。

I am trying to validate my text fields. I have two textfields Username and Password. I want to check that if the username textfield is kept empty then do not allow the user to move to the password textfield until he enters the username. Any sample code to do this will be much appreciated.

Thanks in advance.

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

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

发布评论

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

评论(4

生生漫 2024-10-15 10:45:41

假设您有某种提交按钮,我不建议尝试人为地将用户保留在一个字段中。而是设置 submitButton.enabled = NO 直到两个字段都有效。如果您指示这些字段是必填字段,那么用户将不会像无法更改文本字段焦点时那样感到困惑。

为了进行验证,在 IB 中将两个文本字段的 EditingChanged 设置为指向 viewContoller 中的方法,例如 ValidateEntry。给他们两个独特的标签,只是 1 和 2 或者你有什么。

在 ValidateEntry 字段中确定正在编辑哪个字段 - (id)sender 将有一个标记告诉您哪个字段是哪个字段 - 当两个字段都有效时使提交按钮可用,submitButton.enabled = YES.如果其中一个无效,请将其禁用。

在我看来,这更加友好,并且结果完全相同:不发送空字段。

Assuming you have some kind of submit button, I don't recommend trying to artificially keep the user in one field. Instead set the submitButton.enabled = NO until both fields are valid. If you indicate that the fields are required then the user won't be nearly as confused as he would be if he couldn't change the text field focus.

For validation, in IB set both textfields' EditingChanged pointing to a method in your viewContoller, something like ValidateEntry. Give them both unique tags, just 1 and 2 or what have you.

In the ValidateEntry field determine which one is being edited -- the (id)sender will have a tag telling you which is which -- and when both fields are valid make the submit button available, submitButton.enabled = YES. If either is invalid, disable it.

In my opinion this is much friendlier and results in exactly the same thing: no sending empty fields.

掀纱窥君容 2024-10-15 10:45:41

在 viewDidLoad 中禁用密码 textField

yourPassWordTextField.enabled=NO;

使用 declair 在 textField 上添加通知

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver:self
                           selector:@selector (handle_TextFieldTextChanged:)
                               name:UITextFieldTextDidChangeNotification
                             object:self.yourUserIdTextField];

- (void) handle_TextFieldTextChanged:(id)notification {

    if(![yourUserIdTextField.text isEqualToString:@""])
    {   
          yourPassWordTextField.enabled=YES;
    }
       else
      yourPassWordTextField.enabled=NO;
}

并通过在 .h 文件中

-(void)registerForTextFieldNotifications;

disable your password textField in viewDidLoad

yourPassWordTextField.enabled=NO;

and add a notification on textField by using

NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];

    [notificationCenter addObserver:self
                           selector:@selector (handle_TextFieldTextChanged:)
                               name:UITextFieldTextDidChangeNotification
                             object:self.yourUserIdTextField];

- (void) handle_TextFieldTextChanged:(id)notification {

    if(![yourUserIdTextField.text isEqualToString:@""])
    {   
          yourPassWordTextField.enabled=YES;
    }
       else
      yourPassWordTextField.enabled=NO;
}

in .h file

-(void)registerForTextFieldNotifications;

declair this also.

画尸师 2024-10-15 10:45:41

您将用户名文本字段设置为firstResponder。然后在Textfield中开始编辑方法检查是否输入了字符。在textfieldDidEndEditing中,检查文本字段是否不为空。如果不为空,则允许密码字段成为第一响应者,否则将第一响应者保留为用户名文本字段本身。

you make your username textfield as firstResponder.Then in Textfield did beginediting method check if characters entered or not.And in textfieldDidEndEditing,check if the textfield is not empty.If its not empty then allow password field to become first responder else keep first responder as username textfield itself.

相思碎 2024-10-15 10:45:41

您想在 –textFieldDidBeginEditing: 委托中检查这一点

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField==passwordField)
{
if([userField.text isEqualToString:@""])
//Dont leave user field empty
}
}

You want to check this in – textFieldDidBeginEditing: delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
if(textField==passwordField)
{
if([userField.text isEqualToString:@""])
//Dont leave user field empty
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文