Limit the input amount of UITextField (which can be the correct amount of decimal places), and uitextfield decimal places

Source: Internet
Author: User

Limit the input amount of UITextField (which can be the correct amount of decimal places), and uitextfield decimal places

There are two methods to determine whether the input amount is the correct amount. One is to use a regular expression, and the other is to use the textfield proxy method.

Sometimes it is inevitable that such a requirement will not be input if the amount does not conform to the rules. This method is more reasonable.

If the Input Keyboard is set to Decimal Pad, enter a Number and a Decimal point. If the Input Keyboard is set to Number Pad, enter a pure Number.
In none of the above cases, it is necessary to determine whether the input is reasonable, such as in the code

(Single> = '0' & single <= '9') | single = '.'

If you only enter a number or a decimal point, you can remove this criterion.

The input amount is limited as follows:

1. the user must enter the first digit not a decimal point;

2. The number of decimal places cannot exceed two;

3. If the first digit is 0, only the decimal point can be entered.

4. The input amount cannot exceed 11 characters

When the first input field cannot be 0, you can add a field where the first field cannot be ".". You can modify it as needed.

if(single == '.'){  [textField.text stringByReplacingCharactersInRange:range withString:@""];  return NO;                        }

However, this amount can be 0.00 or the like. If the input is needed, you must determine whether the null value and the input minimum amount are allowed before the request data occurs. If the input amount does not match, return;

Restrictions:

1-(BOOL) textField :( UITextField *) textField shouldChangeCharactersInRange :( nsange) range replacementString :( NSString *) string 2 {3 if (textField. text. length> 10) {4 return range. location <11; 5} else {6 BOOL isHaveDian = YES; 7 if ([textField. text rangeOfString :@". "]. location = NSNotFound) {8 isHaveDian = NO; 9} 10 if ([string length]> 0) 11 {12 unichar single = [string characterAtIndex: 0]; // The character 1 currently entered 3 14 if (single> = '0' & single <= '9') | single = '. ') // the data format is correct 15 {16 // The first letter cannot be a decimal point 17 if ([textField. text length] = 0) {18 if (single = '. ') {19 [textField. text stringByReplacingCharactersInRange: range withString: @ ""]; 20 return NO; 21 22} 23} 24 if ([textField. text length] = 1 & [textField. text isEqualToString: @ "0"]) {25 if (single! = '. ') {26 [textField. text stringByReplacingCharactersInRange: range withString: @ ""]; 27 return NO; 28 29} 30} 31 if (single = '. ') 32 {33 if (! IsHaveDian) // no decimal point 34 {35 isHaveDian = YES; 36 return YES; 37} else38 {39 [textField. text stringByReplacingCharactersInRange: range withString: @ ""]; 40 return NO; 41} 42} 43 else44 {45 if (isHaveDian) // The number of decimal places is 46 {47 // The number of digits to determine the decimal point is 48. nsange ran = [textField. text rangeOfString :@". "]; 49 NSInteger tt = range. location-ran.location; 50 if (tt <= 2) {51 return YES; 52} else {53 return NO; 54} 55} 56 else57 {58 return YES; 59} 60} 61} else {// the input data format is incorrect 62 [textField. text stringByReplacingCharactersInRange: range withString: @ ""]; 63 return NO; 64} 65} 66 else67 {68 return YES; 69} 70} 71}

 

Related Article

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.