-3

我在将文本框限制为仅数值时遇到问题。我使用了普通方法,但它只适用于 Windows,不适用于 ipod,它允许用户输入字符串和特殊字符。,如果您知道如何在 iOS 上限制这一点,请帮助,

核实....

if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || event.keyCode == 116 ||
            (event.keyCode == 65 && event.ctrlKey === true) ||
            ((event.keyCode >= 35 && event.keyCode <= 45) || event.keyCode == 190 || event.keyCode == 100 || event.keyCode == 110)) {
                if ((event.keyCode == 190 || event.keyCode == 100 || event.keyCode == 110) && $(this).val().indexOf('.') != -1) {
                    event.preventDefault();
                }
                else {return;}
        }
        else {
            // Ensure that it is a number and stop the keypress
            if ((event.keyCode >= 219 && event.keyCode <= 222) || (event.keyCode >= 106 && event.keyCode <= 109) || event.keyCode == 111
             || event.keyCode == 32 || event.keyCode == 59 || (event.keyCode >= 186 && event.keyCode <= 189) || event.keyCode == 191 || event.keyCode == 192 ||
        event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || (event.keyCode > 105 && event.keycode < 190 )) ){
            /*
             if (checker.apple) {
                // alert('Hh');
                 alert(event.keyCode);
        //$.browser.apple=true;
        event.shiftKey || (event.keyCode <= 48 || event.keyCode >= 57) && (event.keyCode < 96 || (event.keyCode > 105 && event.keycode < 190 ))
        event.preventDefault();
    }*/
                //alert(event.keyCode);
                /*event.preventDefault();
            }
        } 
4

3 回答 3

0

您可以onkeypress()在用户按下任何按钮并触发 checkAlpha() 函数时使用。

并使用以下代码来检测它是数字还是字母

function checkAlpha() {
x = event.charCode;
if (( x >= 65  && x <=90) || (x >= 97 && x <=122))
{
alert("Yes, it is alphabet");
}
else
{
alert("Sorry, only alphabet allowed");
event.preventDefault();
}
}

工作正常最新的 android 和桌面请在苹果设备上确认并让我知道。

于 2012-12-27T05:27:59.280 回答
0

请按照此代码在文本框中仅输入数值。确保这些DELEGATE方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
char *x = (char*)[string UTF8String];
//if(DEBUG_MODE) NSLog(@"char index is %i",x[0]);
if([string isEqualToString:@"0"] || [string isEqualToString:@"1"] ||  [string isEqualToString:@"2"] ||  [string isEqualToString:@"3"] ||  [string isEqualToString:@"4"] ||  [string isEqualToString:@"5"] ||  [string isEqualToString:@"6"] ||  [string isEqualToString:@"7"] ||  [string isEqualToString:@"8"] ||  [string isEqualToString:@"9"] || x[0]==0 ) {

    NSUInteger newLength = [textField.text length] + [string length] - range.length;
    return (newLength > 6) ? NO : YES;
} else {
    return NO;
}
}
于 2012-12-27T05:32:00.193 回答
0

限制用户只输入数字:

  - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        if ([string length] == 0 && range.length > 0)
        {
            textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
            return NO;
        }

        NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
        if ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0)return YES;

        return NO;
    }
于 2012-12-27T05:35:54.483 回答