简体   繁体   English

javascript防止textarea中的特殊字符

[英]javascript preventing special characters in a textarea

i need to restrict the usage of the following special characters <>{} in a textarea. 我需要限制textarea中以下特殊字符<>{}的使用。 I am making use of this Alphanumeric Plugin for jQuery ( https://github.com/KevinSheedy/jquery.alphanum ). 我正在使用这个用于jQuery的字母数字插件( https://github.com/KevinSheedy/jquery.alphanum )。

My script is as follows: 我的脚本如下:

$(document).on('keyup', '#ta_0', function(e){
    blacklisted_characters = $('#ta_0').alphanum({ disallow : '<>{}' }); // Specify characters to disallow
    if (blacklisted_characters) {
        if (!!$.prototype.fancybox)
            $.fancybox.open([
                {
                    type: 'inline',
                    autoScale: true,
                    minHeight: 30,
                    content: '<p class="fancybox-error">' + 'Désolé, l\'utilisation de ce caractère n\'est pas autorisée.' + '</p>'
                }],
                {
                    padding: 0
                });
        else
            alert('Désolé, l\'utilisation de ce caractère n\'est pas autorisée.');
    };

});

The pop up should appear in case those special characters has been written in the textarea. 如果在文本区域中写入了这些特殊字符,则应出现弹出窗口。 But i think there is an error in my code, because its preventing me from adding texts in the textarea and poping up the error message everytime. 但是我认为我的代码中有一个错误,因为它阻止了我在textarea中添加文本并每次都弹出错误消息。

The plugin works with this only: 该插件仅与此兼容:

$('#ta_0').alphanum({ 
    disallow           : '<>{}' 
});

It is the if condition that i am doing wrong. 这是如果我做错了的条件。 The id of my textarea is $ta_0 .I just need to make an if condition so identify if the following characters are written in the textarea <>{} . 我的textarea的id是$ta_0 。我只需要设置一个if条件,以便确定是否在textarea <>{}中写入了以下字符。 If those characters are written in the textarea, it should fire up the error popup. 如果这些字符写在textarea中,它将触发错误弹出窗口。

Any help will be very appreciated. 任何帮助将不胜感激。

Thank you 谢谢

The function: $('#ta_0').alphanum({ disallow : '<>{}' }); 函数: $('#ta_0').alphanum({ disallow : '<>{}' }); does not return the input character as a value, it returns an array of inputs that have been selected using the jQuery selector, in this case #ta_0 . 不返回输入字符作为值,它返回使用jQuery选择器选择的输入数组,在本例中为#ta_0 Since those inputs exist on your page, it will always return true, which causes the popup to continue to run. 由于这些输入存在于您的页面上,因此它将始终返回true,这将导致弹出窗口继续运行。

You'll have to restructure your code in a few ways. 您将不得不以几种方式重组代码。

  1. Move $('#ta_0').alphanum({ disallow : '<>{}' }); 移动$('#ta_0').alphanum({ disallow : '<>{}' }); outside the keyup function 按键功能之外
  2. Find the keycodes for your blacklisted inputs (<>{}) 查找列入黑名单的输入的键码(<> {})
  3. Check the event (in this case e ) inside your keyup function against those keycodes to decide whether to display the fancybox. 根据这些键码检查keyup函数内部的事件 (在这种情况下为e ),以决定是否显示fancybox。

To clarify step 3: It looks like it would be difficult to do this with keyup , because the characters all require the Shift key as a modifier. 要澄清第3步:使用keyup似乎很难做到这一点,因为所有字符都需要Shift键作为修饰符。 But using keypress , you get a unique code for Shift+key . 但是使用keypress ,您会获得Shift + key的唯一代码。

$(document).on("keypress", "#ta_0", function(event) {
  blacklisted_characters = [
    123, // {
    125, // }
    60, // <
    62 // >
  ];
  if (blacklisted_characters.indexOf(event.which) >= 0) {
    alert("Désolé, l\'utilisation de ce caractère n\'est pas autorisée.");
  }
});

I tried to make a fiddle combining this with the alphanum plugin, but there seemed to be a problem loading github user content. 我试图将它与alphanum插件结合起来,但是加载github用户内容似乎有问题。 But here you can see an example of the code above 但是在这里您可以看到上面的代码示例

Ok it's not elegant, and you have to tweek it a little to make it better, but it does what I believe you need: 好的,它并不优雅,您必须对其进行一些调整以使其更好,但是它确实满足了您的需求:

$("#myText").on("keyup",function(event){
    //console.log(event.which);
    if(event.which == 219 || event.which == 221 || event.which == 188 || event.which == 190)
        {
            $("#myText").val($("#myText").val().replace(/<|>|{|}/gi,""));
        }
});

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM