我在将文本框限制为仅数值时遇到问题。我使用普通方法,但它只适用于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();
}
}
答案 0 :(得分:0)
当用户按下任何按钮并激活checkAlpha()函数时,您可以使用onkeypress()
。
并使用以下代码来检测它是数字还是字母
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和桌面请在苹果设备上确认并告诉我。
答案 1 :(得分: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;
}
}
答案 2 :(得分:0)
将用户限制为仅键入In Numbers:
- (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;
}