Javascript String.fromCharCode返回错误的值

时间:2015-05-11 07:12:26

标签: javascript knockout.js

我有这个Knockout自定义绑定来验证文本框只包含英文字母。但是,似乎Javascript的String.fromCharCode返回了错误的值。

例如,希伯来字母“ש”以英文字母“A”返回,而右边数字键盘中的数字“1”返回为“a”...

这是我的Knockout绑定:

var arrValidKeys = [8, 16, 17, 20, 35, 36, 37, 39, 46];
    ko.bindingHandlers.validateText = {
        init: function (element, valueAccessor) {
            $(element).on("keydown", function (event) {

                //Regex pattern: allow only (A to Z uppercase, a to z lowercase)
                var englishAlphabet = /[A-Za-z]/g;

                // Retrieving the key from the char code passed in event.which
                var key = String.fromCharCode(event.which);

                // keyCodes list: http://stackoverflow.com/a/3781360/114029
                // check that the key is valid with the above Regex
                valueAccessor()($(this).val());

                return ((jQuery.inArray(event.keyCode, arrValidKeys) != -1) || englishAlphabet.test(key));
            });

            $(element).on("keyup", function (event) {

                //Regex pattern: allow only (A to Z uppercase, a to z lowercase)
                var englishAlphabet = /[A-Za-z]/g;

                // Retrieving the key from the char code passed in event.which
                var key = String.fromCharCode(event.which);

                // keyCodes list: http://stackoverflow.com/a/3781360/114029
                // check that the key is valid with the above Regex

                valueAccessor()($(this).val());

                return ((jQuery.inArray(event.keyCode, arrValidKeys) != -1) || englishAlphabet.test(key));
            });

            $(element).on("paste", function (e) {
                var englishAlphabet = /[A-Za-z]/g;
                if (englishAlphabet.test($(this).val()))
                    valueAccessor()($(this).val());
                else
                    e.preventDefault();
            });
        }
    };

1 个答案:

答案 0 :(得分:0)

关注@Jeanluca Scaljeri对此帖的建议:Get Correct keyCode for keypad(numpad) keys

我将我的代码更新为:

// keyCodes list: https://stackoverflow.com/a/3781360/114029
var arrValidKeys = [8, /*backspace*/
                    16, /*shift*/
                    17, /*ctrl*/
                    20, /*caps lock*/
                    35, /*end*/
                    36, /*home*/
                    37, /*left arrow*/
                    39, /*right arrow*/
                    46, /*delete*/];

function validateChar(keyCode) {
    //Regex pattern: allow only (A to Z uppercase, a to z lowercase)
    var englishAlphabet = /[A-Za-z]/g;

    //Retrieving the key from the char code passed in event.which
    var key = String.fromCharCode(keyCode);

    //Check that the key is valid with the above Regex
    return ((jQuery.inArray(keyCode, arrValidKeys) != -1) ||     englishAlphabet.test(key));
}

ko.bindingHandlers.validateText = {
    init: function (element, valueAccessor) {
        $(element).on("keypress", function (event) {
            valueAccessor()($(this).val());
            return validateChar(event.which);
        });

        $(element).on("keydown", function (event) {
            valueAccessor()($(this).val());
            return validateChar(event.which);
        });

        $(element).on("keyup", function (event) {
            valueAccessor()($(this).val());
            return validateChar(event.which);
        });

        $(element).on("paste", function (e) {
            var englishAlphabet = /^[A-Za-z]+$/g;
            var text = e.originalEvent.clipboardData.getData('Text');
            if (!englishAlphabet.test(text))
                e.preventDefault();
        });
    }
};

我添加了所有3个事件(keypresskeydownkeyup)的原因是,当按下退格键时,keypress单独不会触发,而我需要它用knockout observable更新标签。

这是一个有用的jsfiddle链接:https://jsfiddle.net/2ma9nhn7/6/