Lir*_*man 5 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();
});
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1060 次 |
| 最近记录: |