gpt4 book ai didi

Javascript String.fromCharCode 返回错误值

转载 作者:搜寻专家 更新时间:2023-11-01 04:41:03 25 4
gpt4 key购买 nike

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

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

这是我的 Knockout 绑定(bind):

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();
});
}
};

最佳答案

遵循@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 的标签。

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

关于Javascript String.fromCharCode 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30161605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com