UI文本字段验证
我正在尝试验证我的文本字段。我有两个文本字段用户名和密码。我想检查是否用户名文本字段保留为空,然后不允许用户移动到密码文本字段,直到他输入用户名。任何执行此操作的示例代码将不胜感激。
提前致谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您有某种提交按钮,我不建议尝试人为地将用户保留在一个字段中。而是设置
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 likeValidateEntry
. 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.
在 viewDidLoad 中禁用密码 textField
使用 declair 在 textField 上添加通知
并通过在 .h 文件中
。
disable your password textField in viewDidLoad
and add a notification on textField by using
in .h file
declair this also.
您将用户名文本字段设置为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.
您想在
–textFieldDidBeginEditing:
委托中检查这一点You want to check this in
– textFieldDidBeginEditing:
delegate