Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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
Javascript 正则表达式后覆盖html输入字段中的文本_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 正则表达式后覆盖html输入字段中的文本

Javascript 正则表达式后覆盖html输入字段中的文本,javascript,jquery,regex,Javascript,Jquery,Regex,我有几个需要过滤输入的输入字段。 我将它们放在一个类中,并使用正则表达式将字符限制为数字、字母和下划线。 这很好,但是当我从一个输入字段切换到下一个输入字段时,光标会移动到输入文本的末尾。我希望它被高亮显示,这样它就可以在需要时被输入,而不必先用鼠标高亮显示 <input type="input" class="jqfc" value="one"><br> <input type="input" class="jqfc" value="two"><br&

我有几个需要过滤输入的输入字段。 我将它们放在一个类中,并使用正则表达式将字符限制为数字、字母和下划线。 这很好,但是当我从一个输入字段切换到下一个输入字段时,光标会移动到输入文本的末尾。我希望它被高亮显示,这样它就可以在需要时被输入,而不必先用鼠标高亮显示

<input type="input" class="jqfc" value="one"><br>
<input type="input" class="jqfc" value="two"><br>    
<input type="input" class="jqfc" value="three"><br>    
<input type="input" value="highlights"><br>    


jQuery('.jqfc').keyup(function () {
      this.value = this.value.replace(/[^a-z0-9\_]/gi, "");
});




jQuery('.jqfc').keyup(函数(){ this.value=this.value.replace(/[^a-z0-9\\\\]/gi,”); });
样本:


这样,如果按下tab键,它将不会运行逻辑。我想做一些类似于
select()
,但每次键入时都会发生这种情况。

这应该可以做到:

jQuery('.jqfc').keyup(function () {
    var regex = /[^a-z0-9\_]/gi;
    if(this.value.match(regex)){
        this.value = this.value.replace(regex, "");
    }

});

jQuery('.jqfc').on('focus, click', function(){
        this.select();
});

jQuery('.jqfc').keyup(function () {
    var regex = /[^a-z0-9\_]/gi;
    if(this.value.match(regex)){
        this.value = this.value.replace(regex, "");
    }

});

jQuery('.jqfc').on('focus, click', function(){
        this.select();
});