Use the Uitextfield limit to enter the amount is the correct decimal _android

Source: Internet
Author: User

To determine the amount of input is the correct amount of methods there are two, one is a regular expression, the other is a TextField proxy method

Sometimes it is inevitable to encounter such a demand, does not conform to the rules of the amount of input, it is more reasonable to use this method

If you set the input keyboard to decimal pad, enter a number and a decimal point; if you set the keyboard to number pad, enter as a pure digit

In the case of the above, it is necessary to determine whether the input is reasonable input, such as in the code

Copy Code code as follows:

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

If it's just an input number or a decimal point, then you can remove the criterion.

The following limit input amount is very dead:

1. Require users to enter the first point can not be decimal;

2. No more than two digits after the decimal point, unable to enter more than one decimal point;

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

4. Input amount not exceeding 11 digits

This input first cannot be 0 o'clock and can be in the first place and cannot be "." Place Plus, can be modified according to your own needs.

if (single = = '. ')  {
[Textfield.text stringbyreplacingcharactersinrange:range withstring:@ "];
return NO; 
}

But this amount can enter 0.00 or so, the input needs to be in the case of the request data to judge empty and enter the minimum amount allowed, do not meet when return;

The restrictions are as follows:

-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) String {if (TextField.text.length >) {return range.location <} else{BOOL Ishavedian = YES; if ([Textfield.text rangeofstring:@ "."]. Location==nsnotfound) {Ishavedian=no} if ([string length]>) {Unichar single=[string characteratindex:];//the currently entered character I F (Single >= ' && single<= ') | | single== '. ') The data format is correct {//The first letter cannot be a decimal point if ([Textfield.text length]==) {if (single = =)
{[Textfield.text stringbyreplacingcharactersinrange:range withstring:@ "]; return NO;} } if ([Textfield.text length]== && [Textfield.text isequaltostring:@ "]) {if (single!= '. ')
{[Textfield.text stringbyreplacingcharactersinrange:range withstring:@ "]; return NO;}
if (single== '. ') {if (!ishavedian)//text does not yet have a decimal point {ishavedian=yes; return YES}
else {[Textfield.text stringbyreplacingcharactersinrange:range withstring:@ ']; return NO;}} else {if (Ishavedian)//There is a decimal point {//Determine the number of digits of the decimal point nsrange ran=[textfield.text rangeofstring:@ "."];
Nsinteger tt=range.location-ran.location; if (TT <=) {return YES;}
else{return NO;}
else {return YES}}
}else{//entered data incorrectly formatted [Textfield.text stringbyreplacingcharactersinrange:range withstring:@ "]; return no;}}
else {return YES}}  }

The above is a small set to introduce the above is a small set to introduce the use of Uitextfield Limited input amount is the correct decimal knowledge, I hope to

Help, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.