我正在使用以下代码来防止某些字符被输入到文本框中。除了 iPad 和 iPhone 上的 Chrome 和 Safari(webkit 移动设备?)外,它运行良好。在 Safari 和 Chrome 中的 Mac 上运行良好。任何想法如何限制这些设备的数字和冒号?
jQuery.fn.forceNumeric = function (allowDecimal, allowColon)
{
return this.each(function ()
{
$(this).keydown(function (e)
{
var key = e.which || e.keyCode;
if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
// numbers
key >= 48 && key <= 57 ||
// Numeric keypad
key >= 96 && key <= 105 ||
// period, comma, minus adn period on keypad
// key == 190 || // period
// key == 188 || // comma
// key == 109 || // minus
// key == 110 || // period on keypad
// Backspace and Tab and Enter
key == 8 || key == 9 || key == 13 ||
// Home and End
key == 35 || key == 36 ||
// left and right arrows
key == 37 || key == 39 ||
// Del and Ins
key == 46 || key == 45)
return true;
else if (!e.shiftKey && !e.altKey && !e.ctrlKey && allowDecimal && key == 190) // period
return true;
else if (!e.shiftKey && !e.altKey && !e.ctrlKey && allowDecimal && key == 110) // period on keypad
return true;
else if (e.shiftKey && (key == 186 || key == 59) && allowColon) // colon (ie & chrome = 186, ff = 59)
return true;
else
return false;
});
});