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;
}
}