How to Test Character Input to UITextField as the User Enters Characters and Prevent Invalid Characters

前端 未结 6 1924
深忆病人
深忆病人 2020-12-02 16:22

First, I setup up the keyboard for the UITextField to use the number with decimal style. So the user can only enter numbers and a single decimal.

What I want to do i

6条回答
  •  爱一瞬间的悲伤
    2020-12-02 16:57

    Good! If someone need it working with non-US currencies: euro or other ones with fraction separated by comma instead of dot use this one:

    NSString *expression = @"^([0-9]+)?([\\,\\.]([0-9]{1,2})?)?$";
    

    The only difference is that it allows comma or dot. ([\,\.] - either . or ,) Only trick is that you need to replace comma with dot when using your acquired number, because computer uses dot to separate fraction, not the comma.

提交回复
热议问题