iOS Development Uitextfield (input box) limit you can only enter integers and decimals

Source: Internet
Author: User

My limit is 8 digits after the decimal point.


-(BOOL) TextField: (Uitextfield *) TextField Shouldchangecharactersinrange: (nsrange) range replacementstring: ( NSString *) string{//limit only digits can be entered
BOOL Ishavedian = YES;
if ([String isequaltostring:@ ""]) {
return NO;
}
if ([Textfield.text rangeofstring:@ "."]. Location = = Nsnotfound) {
Ishavedian = NO;
}
if ([string length] > 0) {

Unichar single = [string characteratindex:0];//the character currently entered
if (single >= ' 0 ' && single <= ' 9 ') | | | {//Data format is correct

if ([textfield.text length] = = 0) {
if (single = = '. ') {
[Textfield.text stringbyreplacingcharactersinrange:range withstring:@ "];
return NO;
}
}
Whether the character entered is a decimal point
if (single = = '. ') {
There are no decimal points in the IF (!ishavedian)//text
{
Ishavedian = YES;
return YES;

}else{
[Textfield.text stringbyreplacingcharactersinrange:range withstring:@ "];
return NO;
}
}else{
if (Ishavedian) {//Decimal

To determine the number of digits of a decimal point
Nsrange ran = [Textfield.text rangeofstring:@ "."];
if (range.location-ran.location <= 8) {
return YES;
}else{
return NO;
}
}else{
return YES;
}
}
The data entered by}else{//is not in the correct format
[Textfield.text stringbyreplacingcharactersinrange:range withstring:@ "];
return NO;
}
}
Else
{
return YES;
}
}

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.