美文网首页
TextFiled仅输入数字

TextFiled仅输入数字

作者: fankang | 来源:发表于2016-08-17 10:32 被阅读8次

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

    {

    return [self validateNumber:string];

    }

    - (BOOL)validateNumber:(NSString*)number {

    BOOL res = YES;

    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];

    int i = 0;

    while (i < number.length) {

    NSString * string = [number substringWithRange:NSMakeRange(i, 1)];

    NSRange range = [string rangeOfCharacterFromSet:tmpSet];

    if (range.length == 0) {

    res = NO;

    break;

    }

    i++;

    }

    return res;

    }

    相关文章

      网友评论

          本文标题:TextFiled仅输入数字

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