0
function ord(string) {
    var str = string + '',
        code = str.charCodeAt(0);
    if (0xD800 <= code && code <= 0xDBFF) { // High surrogate (could change last hex to 0xDB7F to treat high private surrogates as single characters)
        var hi = code;
        if (str.length === 1) {
            return code; // This is just a high surrogate with no following low surrogate, so we return its value;
            // we could also throw an error as it is not a complete character, but someone may want to know }
            var low = str.charCodeAt(1);
            return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
        }
        if (0xDC00 <= code && code <= 0xDFFF) { // Low surrogate return code; // This is just a low surrogate with no preceding high surrogate, so we return its value;
            // we could also throw an error as it is not a complete character, but someone may want to know
        }
        return code;
    }
}

$(document).ready(function () {
    var maxTxtNumber = 8;
    var arrTxtNumber = new Array();
    var txtvalues = new Array();
    var arr = {};

    $('.numericonly').keypress(function (e) {
        var t = $(this).val();
        var k = e.which;
        delete arr[8];
        if ((e.which >= 49 && e.which <= 55) || e.which == 8) {
            if (e.which == 8) {
                var s = new String(t);
                s = s.charCodeAt(0);
                delete arr[s];
            }
            if (arr[k]) {
                e.preventDefault();
            } else {
                arr[k] = e.which;
            }
        } else {
            e.preventDefault();
        }
    });
});

该代码适用于firefox,但不适用于IE和chrome?

先生/女士,您的回答会很有帮助。谢谢++

4

3 回答 3

0

不需要所有这些代码。如果您想测试输入的值是否仅为数字,则可以执行以下操作:

<input type="text" onblur="check(this);" ...>


function check(el) {
  if (!isDigits(el.value)) {
    alert('Hey!!\nThe element you just left should only contain digits');
  }
}

function isDigits(s) {
  return /^\d*$/.test(s);
}

在提供有关无效值的警告之前,向用户提示您需要的格式并等到他们离开控件或提交表单时,会更加友好。您真的不在乎用户如何获得有效值,只要它在提交表单时有效即可。

而且您必须再次在服务器上进行验证。

于 2012-08-06T03:26:11.497 回答
0

我建议通过验证器(例如http://www.jslint.com/ )运行您的代码,以确保一切都符合通用标准。

于 2012-08-06T00:46:42.787 回答
0

其他浏览器e.keyCode用来告诉您按下了哪个键。跨浏览器:

var k = e.keyCode || e.which;

还要确保每次都使用k而不是重复。e.which

于 2012-08-06T00:47:10.557 回答