Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 消除小写字符的表单验证正则表达式_Jquery_Regex_Webforms - Fatal编程技术网

Jquery 消除小写字符的表单验证正则表达式

Jquery 消除小写字符的表单验证正则表达式,jquery,regex,webforms,Jquery,Regex,Webforms,在文本框中输入除小写字母以外的任何字符时,尝试获取类更改。然而,我确信有一些愚蠢的小事情导致了这一点,它没有显示console.log的“yo!”。下面是javascript: $(function(){ $("input[name='name']").keyup(function(){ var str = $(this).val(); var badChars = new RegExp("[^a-z]"); if (str.indexOf

在文本框中输入除小写字母以外的任何字符时,尝试获取类更改。然而,我确信有一些愚蠢的小事情导致了这一点,它没有显示console.log的“yo!”。下面是javascript:

$(function(){
    $("input[name='name']").keyup(function(){
        var str = $(this).val();
        var badChars = new RegExp("[^a-z]");
        if (str.indexOf(badChars)!=-1){
            console.log("yo!");
            $(this).removeClass("good");
            $(this).addClass("error");
        }
    });
});
我搞砸了什么

$(function() {
    // Don't need to create new RegExp object on each keyup event.
    // Just create it once:
    var badChars = new RegExp("[^a-z]");

    $("input[name='name']").keyup(function() {
        // Use RegExp.test() method to check 
        // is string matches the regular expression.
        if (badChars.test(this.value)) {
            console.log("yo!");
            $(this).removeClass("good")
                   .addClass("error");
        }
    });
});

使用name还是ID更好?str.indexOf接受字符串而不是正则表达式。您可能需要匹配或测试。