Uitextfield input in iOS to determine the two digits after the decimal point

Source: Internet
Author: User

In the Uitextfield of the input amount, three rules should be judged.
1. only numbers can be entered (by setting the keyboard type to decimal pad)
2. The decimal point can only have one
3. up to two digits after the decimal point (can be judged by regular expression or length)

2. and 3. Code (first introduce uitextfielddelegate, specify proxy for yourself)

- (BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (Nsrange) Range Replacementstring: (NSString *) string{if (TextField = =Self. Tfwithdrawalamount) {If you entered "." Before judging, there is already "." Or the string is emptyif ([String isequaltostring:@"."] && ([TextField. Text rangeofstring:@"."]. location! =Nsnotfound | | [TextField.text isequaltostring:@ ""])) {return NO;} //spell out the input to complete str, judging the length of STR is greater than or equal to "." Position +4, returns false, the insertion of string failed ("379132.424", length 10, ".") Position 6, 10>=6+4) nsmutablestring *str = [[NSMutableString Alloc] Initwithstring:textfield.text]; [Str insertstring:string atindex:range.location]; if (Str.length >= [str rangeofstring:@". .location+4) {return NO; }} return yes;}        

Uitextfield Input in iOS determines two digits after decimal point

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.