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