javascript - 在 keyup 和 regex 上测试用户输入

标签 javascript jquery

在我的例子中,要求是这样的——

名字应该允许字母,一些字符,如逗号、破折号和上升字符。

当我们尝试粘贴上升字符或在 Firefox 中使用“abctajpu”时,代码工作正常。但是只要用户输入 ALT+0192 或任何带有数字键盘的 ALT 键。

keyup 函数不起作用。它允许用户使用 ALT 键键入所有可能的组合。

这是示例代码..

var namePattern = /^[a-zA-Z,-. \'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŠšŽžÅå]$/g;
var negateNamePattern = /[^a-zA-Z,-. \'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŠšŽžÅå]/g;

$("#First_Name").bind('keyup paste altKey', function(event) {
  var obj = $(this);
  if (event.type == 'paste') {
     setTimeout(function() {
         validateRealTime(event, obj, namePattern, negateNamePattern)
     }, 1);
  } else {
   validateRealTime(event, obj, namePattern, negateNamePattern);
  }
});

最佳答案

key up 事件仍然会在他们使用数字键盘执行任何 ALT 键时触发,但是只有当他们停止输入时才会附加新字符。

是否有特定原因需要执行 key up 事件? 当他们失去对输入的关注时,您可以验证您的字段。

此外,我们可以看到您的 validateRealTime 函数吗?

编辑

我想我已经想出了一个方法来完成你想要的。

我们必须更改您的函数以验证传递的字符串而不是获取对象的值:

function validateRealTime(str, regExPattern, negateRegExPattern) { 
    var fieldVal = str;
    fieldVal = fieldVal.replace(/^(\s+)/g,''); 
    fieldVal = validateInput(fieldVal, regExPattern, negateRegExPattern);  
    // return the new and validated value
    return fieldVal; 
}

我已经从函数中删除了 event,但如果您在其他地方使用它,您可以添加它。

此外,我们需要将 keyup 事件更改为 keypress 事件。这将使我们能够确定何时按下 ALT(ALT + # = 一次按键,而 ALT + # = # keyups)

此外,由于输入值在 keyup 上更新,我们需要确保在 keyup 上只显示经过验证的字符串。

var validatedValue;    // Store validate value to display on 'keyup'
var namePattern = /^[a-zA-Z,-. \'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŠšŽžÅå]$/g;
var negateNamePattern = /[^a-zA-Z,-. \'ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŠšŽžÅå]/g;

$("#First_Name").bind('keyup', function(event){
    $(this).val(validatedValue);
})
.bind('keypress paste', function(event){
    var obj = $(this);
    var char;
    var string;
    var newval;


    if (event.type == 'paste'){
         setTimeout(function(){validateRealTime(obj.val(), namePattern, negateNamePattern)}, 1);
    } else {

        // Get the ASCII code of the key pressed and get the Char representation of it
        // When we do an ALT+#, it returns only one ASCII value
        char = String.fromCharCode(event.keyCode); 

        // Get the current string and append the character added by the ALT
        // We need to do this, because the val() is not updated yet, it still contains
        //  the old value
        string = obj.val() + char;

        validatedValue= validateRealTime(string, namePattern, negateNamePattern);

    }
});

另外,我还没有测试过paste事件

关于javascript - 在 keyup 和 regex 上测试用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4819227/

相关文章:

javascript - 4-5 秒后球停止弹跳

javascript - WebGL:INVALID_VALUE:attachShader:没有对象或对象被删除。这暗地里有帮助吗?

javascript - 如何为 jquery 自动完成框设置默认值?

javascript - VS Code 中的 Intellisense - 当前建议

javascript - 将 jQuery 传递给 .animate 回调

jquery - Internet Explorer 8 及更低版本的问题

正则表达式 : How can I surround all words starting with @ with <b> tags?

JavaScript 数组排序

javascript - jquery 会使浏览器变慢吗?

javascript - jQuery - 选择带有 $(this) 的 child