Objective C 中的文本验证
在我的应用程序中,我有一些文本字段......
- 电话号码
- 日期
- 货币
- 电子邮件
我想从文本字段获取 NSString 形式的输入值,并希望确保它满足所需的场景。也就是说,电话号码字段应仅包含数字和 + 符号,日期应为 dd/mm/yyyy 形式,货币可包含 $ 符号,电子邮件应满足有效的电子邮件格式。
我怎样才能在 Objective C 中做到这一点?
In my app, i have some text fields for..
- Phone number
- Date
- Currency
I want to get the input value as NSString from the text field, and want to make sure that it satisfies the required scenario. That is, the phone number field should contains digits and + symbol only, Date in the form of dd/mm/yyyy, Currency may contain $ symbol and email should satisfy a valid email format.
How can i do this in Objective C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于电话号码,您可以设置 textfield.keyboardType = UIKeyboardTypePhonePad;
对于日期,您可以使用 UIDatePicker - http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html
货币,您可以设置您的textfield.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
电子邮件:您可以设置您的textfield.keyboardType =UIKeyboardTypeEmailAddress
,但您仍然必须在用户输入后编写代码以进行正确的验证,如上所述将帮助您显示正确的键盘,但用户仍然可以输入不正确的值...
For phone number you can set your textfield.keyboardType = UIKeyboardTypePhonePad;
For Date you can use UIDatePicker - http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDatePicker_Class/Reference/UIDatePicker.html
Currency, you can set your textfield.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
Email: you can set your textfield.keyboardType =UIKeyboardTypeEmailAddress
but still you will have to write the code for proper validation after user input as above will help you to show proper keyboard but still user can enter incorrect values...