关于如何限定UITextField只能输入一个小数点的代码
2016-09-06 12:33
489 查看
当我们在做支付的时候,有时候需要对输入的的数字进行限制,其中就有对小数点的限制,只能输入一个小数点
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
/*
1.start with zero 0000.....was forbiden
2.have no '.' & the first character is not '0'
3.limit the count of '.'
4.if the first character is '0',then the next one must be '.'
5.condition like "0.0.0"was forbiden
6.limit the num of zero after '.'
*/
if(((string.intValue<0) || (string.intValue>9))){
//MyLog(@"====%@",string);
if ((![string
isEqualToString:@"."])) {
return
NO;
}
return NO;
}
NSMutableString * futureString = [NSMutableString
stringWithString:textField.text];
[futureString insertString:string
atIndex:range.location];
NSInteger dotNum = 0;
NSInteger flag=0;
const NSInteger limited = 2;
if((int)futureString.length>=1){
if([futureString
characterAtIndex:0] == '.'){//the first character can't be '.'
return
NO;
}
if((int)futureString.length>=2){//if
the first character is '0',the next one must be '.'
if(([futureString
characterAtIndex:1] != '.'&&[futureString characterAtIndex:0] == '0')){
return
NO;
}
}
}
NSInteger dotAfter = 0;
for (int i = (int)futureString.length-1; i>=0; i--) {
if ([futureString
characterAtIndex:i] == '.') {
dotNum ++;
dotAfter = flag+1;
if (flag > limited) {
return
NO;
}
if(dotNum>1){
return
NO;
}
}
flag++;
}
if(futureString.length - dotAfter > 7){
//[MBProgressHUD toastMessage:@"超出最大金额"];
return NO;
}
return
YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
/*
1.start with zero 0000.....was forbiden
2.have no '.' & the first character is not '0'
3.limit the count of '.'
4.if the first character is '0',then the next one must be '.'
5.condition like "0.0.0"was forbiden
6.limit the num of zero after '.'
*/
if(((string.intValue<0) || (string.intValue>9))){
//MyLog(@"====%@",string);
if ((![string
isEqualToString:@"."])) {
return
NO;
}
return NO;
}
NSMutableString * futureString = [NSMutableString
stringWithString:textField.text];
[futureString insertString:string
atIndex:range.location];
NSInteger dotNum = 0;
NSInteger flag=0;
const NSInteger limited = 2;
if((int)futureString.length>=1){
if([futureString
characterAtIndex:0] == '.'){//the first character can't be '.'
return
NO;
}
if((int)futureString.length>=2){//if
the first character is '0',the next one must be '.'
if(([futureString
characterAtIndex:1] != '.'&&[futureString characterAtIndex:0] == '0')){
return
NO;
}
}
}
NSInteger dotAfter = 0;
for (int i = (int)futureString.length-1; i>=0; i--) {
if ([futureString
characterAtIndex:i] == '.') {
dotNum ++;
dotAfter = flag+1;
if (flag > limited) {
return
NO;
}
if(dotNum>1){
return
NO;
}
}
flag++;
}
if(futureString.length - dotAfter > 7){
//[MBProgressHUD toastMessage:@"超出最大金额"];
return NO;
}
return
YES;
}
相关文章推荐
- flex textinput 如何限定只能输入一个小数点
- 如何限制文本框只能输入数字,小数点,英文,汉字等各类代码
- C#--关于事件的定义及注册,如何实现在一个文本框中只输入数字或者只能输入0至9十种数字、a至z或A至Z五十二种字符。
- UITextField 只能输入数字、一个小数点、只能有两位小数
- 如何限制文本框只能输入数字,小数点,英文,汉字等各类代码
- 如何使SWT文本只能输入金额(一个小数点及数字)
- <转>如何限制文本框只能输入数字,小数点,英文,汉字等各类代码
- 如何限制文本框只能输入数字,小数点,英文,汉字等各类代码(转载)
- 只能输入数字和一个小数点的代码
- 如何限制文本框只能输入数字,小数点,英文,汉字等各类代码(转载)
- 如何限制文本框只能输入数字,小数点,英文,汉字等各类代码
- 执行这些代码, Edit1只能输入数字,小数点和负号,负号和小数点只能输入一个,负号必须在最前,粘贴的数字必须完全正确.
- 如何限制文本框只能输入数字,小数点,英文,汉字等各类代码(转载)
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- 用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- [转]用正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码
- asp.net 关于只能输入数字和小数点的问题
- 在输入框中只能输入0到9,小数点也不能输入的javascript代码
- js正则表达式限制文本框只能输入数字,小数点,英文字母,汉字等各类代码