6

我有一个文本字段,它只接受以下字符:

允许的字符:[az 0-9 + # - .]

当您提出问题时,这与“标签”字段中的过滤器相同如果用户键入无效字符,我希望当前文本字段值保持不变。我试过了:

$('#post_tags').keypress(function(event){
    var char = String.fromCharCode(event.which)
    var txt = $(this).val()

    if (! txt.match(/[^A-Za-z0-9+#-\.]/)){
        $(this).val(txt.replace(char, ''));
    }
})

为什么它不起作用?谢谢!

4

5 回答 5

13

这对我有用:

$(function(){
    $('#t').keypress(function(e){
        var txt = String.fromCharCode(e.which);
        console.log(txt + ' : ' + e.which);
        if(!txt.match(/[A-Za-z0-9+#.]/)) 
        {
            return false;
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="t" />

于 2013-06-20T18:28:46.827 回答
5
/[^A-Za-z0-9+#-\.]/

这否定了这些字符中的任何一个的匹配。要使其匹配多个字符,您必须在+其中使用 a :

/[^A-Za-z0-9+#-\.]+/
                  ^

现在要匹配整个字符串,您需要添加锚点:

/^[^A-Za-z0-9+#-\.]+$/
 ^                  ^

编辑:好的,似乎-这里也创建了一个从字符#.. 在这种情况下,您可以将其转义,也可以将其放在末尾:

/^[^A-Za-z0-9+#\-\.]+$/

/^[^A-Za-z0-9+#\.-]+$/
于 2013-06-20T18:07:51.937 回答
2

您不需要替换字符,只需要阻止它。

var char = String.fromCharCode(event.which)
if (!char.match(/^[^A-Za-z0-9+#\.\-]+$/)) event.preventDefault();
于 2015-03-20T17:44:23.383 回答
1

Alternate Solution

One thing that might help would be to use the .which field instead. Then simply return false when it doesn't fit. I actually have a huge object full of .which info for all major browsers. It includes arrays you could borrow from it to create something like:

var alphaNumericNcontrols = [ 8,9,13,16,17,18,19,20,32,33,34,35,36,37,38,39,40,44,45,46,145,48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,96,97,98,99,100,101,102,103,104,105 ],
    illegal = {
        reg: [ 106,111,191,220 ],
        shift: [ 56,59,188,190,191,220,222 ]
    }

$(document).on("keydown", "input[type=text]", function(e) {
    var eKey = e.which || e.keyCode;
    if (alphaNumericNcontrols.indexOf(eKey) === -1) return false;
    if (illegal.reg.indexOf(eKey) > -1) return false;
    if (e.shiftKey && illegal.shift.indexOf(eKey) > -1) return false;
});

See Working Example Here

Keep in mind my Object is not perfect and there are some updates I probably need to make to it, but i did my best to establish everything from every possible major browser!

于 2013-06-20T18:27:28.820 回答
0

感谢大家。

实际上我的问题有点困难:过滤无效键并以两种不同的方式启动有效键的 ajax 调用:如果用户按空格/逗号或在他/她仍在打字时。这是代码:

$(document).ready(function(){

$('#post_tags').keypress(function(e){
    var txt = String.fromCharCode(e.which);
    console.log(txt + ' : ' + e.which);

    if(txt.match(/^[^A-Za-z0-9+#\-\.]+$/))
    {
          return false;
    }
})

$('#post_tags').keyup(function(event){
      code = event.which
      var token = String.fromCharCode(code)
      var txt = $(this).val()

              //create a new tag, take it out of textfield
      if (code == 32 || code == 188)
      {
        console.log("AJAX new word keyup")

          $.ajax({
            type: 'get',
            url: '/posts/tags_change',
            dataType: "json",
            data: "query=" + $(this).val(),

            success: function(data) {
            console.log(data)
            $('#post_tags').val('')
            },

            error: function(data) {
                alert("Ajax error")
            }
          });
    }
    else
    {
                    //do autocomplete ajax
        console.log("typing:" + txt)
    }

});

})

不知道这是否 100% 正确!

于 2013-06-20T19:09:17.067 回答