在UITextField中验证货币输入的最佳方法是什么? [英] What's the best way to validate currency input in UITextField?
问题描述
我的应用程序允许用户在 UITextField
控件中输入数值(货币),但遗憾的是我希望的键盘布局不是其中之一-in options,所以我不得不在Interface Builder中选择Numbers& Punctuation选项。这是IB中相应的对话窗口:
My application allows the user to enter a numeric value (currency) in a UITextField
control, but the keyboard layout that I wish was available is unfortunately not one of the built-in options, so I had to choose the "Numbers & Punctuation" option in Interface Builder. Here's the corresponding dialog window in IB:
因此,当我的应用程序询问用户输入时,它显示以下内容:
So when my application asks the user for the input, it is displaying the following:
这完全没问题,但请查看用户可用的所有额外密钥!人们可以很容易地输入12; 56!在文本字段中,我假设我必须以某种方式验证。
Which is perfectly fine, but look at all of the extra keys available to the user! One could easily enter "12;56!" in the text field, and I assume I have to validate that somehow.
所以我的问题是:如何验证输入的货币值? UITextField
?
So my question is: how do I validate currency values entered into a UITextField
?
推荐答案
也许您可以附加 UITextFieldDelegate
在控件上并实现 textField:shouldChangeCharactersInRange:replacementString:
这样,如果它看到任何在字段中你不想要的字符,它可以拒绝它们。
Perhaps you could attach a UITextFieldDelegate
on the control and have it implement textField:shouldChangeCharactersInRange:replacementString:
That way, if it sees any characters that you don't want in the field, it can reject them.
这篇关于在UITextField中验证货币输入的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!