美文网首页
输入框限制输入数字

输入框限制输入数字

作者: CYC666 | 来源:发表于2018-07-20 10:55 被阅读3次

// 限制输入非数字和.

    for(NSIntegerindex =0; index < string.length; index++) {

        unicharc = [stringcharacterAtIndex:index];

        if(c <48&& c !=46) {    // 46是.

            returnNO;

        }

        if(c >57) {

            returnNO;

        }

    }

    NSRangepointRange = [textField.textrangeOfString:@"."];

    if(pointRange.location!=NSNotFound) {

        // 有小数点了

        if([stringisEqualToString:@"."]) {

            // 不允许再输入小数点

            returnNO;

        }

        NSArray*list = [textField.textcomponentsSeparatedByString:@"."];

        if(list.count==2) {

            NSString*str = list.lastObject;

            if(str.length>=2&& ![stringisEqualToString:@""]) {

                // 不能超过两位小数,退格键

                returnNO;

            }

        }

    }

相关文章

网友评论

      本文标题:输入框限制输入数字

      本文链接:https://www.haomeiwen.com/subject/zayfmftx.html