javascript 检查按键上的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31852843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:27:45  来源:igfitidea点击:

Checking regex expression on keypress

javascriptjqueryregex

提问by Adam Mrozek

I would like to create my own plugin (without use any external libraries - it's for learning purpose) to validate text typed by user dynamically in regex test function.

我想创建自己的插件(不使用任何外部库 - 用于学习目的)来验证用户在正则表达式测试函数中动态输入的文本。

In example I have regex pattern:

例如,我有正则表达式模式:

^.{2}$

^.{2}$

And javascript function

和 javascript 函数

$('#textbox').bind("keypress", function (event) {
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
        event.preventDefault();
        return false;
    }
});

Now, I want to type two dots into textbox, but after first keypress is fired, one dot doesn't match to the pattern and nothing happens because event is prevented.

现在,我想在文本框中输入两个点,但是在触发第一次按键后,一个点与模式不匹配,并且没有任何反应,因为事件被阻止。

My question: Is it possible to check if currently typed text matches with regex pattern?

我的问题:是否可以检查当前输入的文本是否与正则表达式匹配?

回答by Fest7

Your regex only accept two dots (..), but you're testing a single character!

您的正则表达式只接受两个点 (..),但您正在测试单个字符!

var regex = new RegExp("^.{2}$");

$('#textbox').bind("keypress", function (event) {
    var key = String.fromCharCode(!event.charCode ? event.which : event.charCode);
    if (!regex.test(key)) {
        event.preventDefault();
        return false;
    }
});

Wanna check current typed text when users finished typing? .. Look HERE

想在用户完成输入时检查当前输入的文本吗?..看这里

回答by Akshay

You can try keydown as it will trigger as soon as you press a key and before character is displayed in textbox. So if it doesnot match with the pattern you can return out.

您可以尝试使用 keydown,因为它会在您按下一个键并在文本框中显示字符之前立即触发。因此,如果它与模式不匹配,您可以返回。

回答by onerror

$('#textbox').bind("keyup", function (event) {
    var user_input = getElementById ("your_input_id_here");
    if (!regex.test(user_input)) {
        return false;
    }
});

So, essentially you should use onkeyup instead and you should check the whole user input, not just the last key

因此,本质上您应该改用 onkeyup 并且您应该检查整个用户输入,而不仅仅是最后一个键

回答by Tamilselvan K

var alpha = /[ A-Za-z]/;
var numeric = /[0-9]/; 
var alphanumeric = /[ A-Za-z0-9]/;

function validateKeypress(validChars) {
    var keyChar = String.fromCharCode(event.which || event.keyCode);
    return validChars.test(keyChar) ? keyChar : false;
}

The HTML will have to change to onkeypress="validateKeypress(alpha);"

HTML 必须更改为 onkeypress="validateKeypress(alpha);"

回答by Harshad Khandagale

var in_put = document.getElementById("in_put"),
key = null;

in_put.addEventListener("keydown", match_input);

function match_input(e){

    console.log("Key: "+e.key);
    key = e.key;
    console.log("Match: "+key.match(/a/i));//return array of match
    if(key.match(/a/i)){

        //code on succession.
    }else{

        //code on failure.
    }
}

Note: Change /a/i with your /pattern/ig. It only check for input is a/A.

注意:将 /a/i 更改为您的 /pattern/ig。它只检查输入是 a/A。