【问题标题】:how to prevent entering and pasting single and double quotes inside cleditor?如何防止在cleditor中输入和粘贴单引号和双引号?
【发布时间】:2016-03-24 22:14:50
【问题描述】:

如何防止在cleditor中输入和粘贴单引号和双引号? 如何防止在cleditor中输入和粘贴单引号和双引号?

<script type="text/javascript">
  $(document).ready(function(){
    $prevent_single_double_quote = function(e){
    var element=e;
    setTimeout(function () { element.val(element.val().replace(/['"]/g, "")); }, 1);
    }
    $('textarea').on('paste', function () {
    $prevent_single_double_quote($(this));
    });
    $('textarea').on('keyup', function () {
    $prevent_single_double_quote($(this));
    });
    $('input').on('paste', function () {
    $prevent_single_double_quote($(this));
    });
    $('input').on('keyup', function () {
    $prevent_single_double_quote($(this));
    });
    $('.scle').on('keyup', function () {
    $prevent_single_double_quote($(this));
    });
});
</script>

 <div class="col-md-9">
                <div class="block block-fill-white" id="mailcontent">
                   <div class="content np" id="mailcontent">
                        <textarea class="scle" name="mailcontent" id="mailcontent"></textarea>
                   </div>
               </div>
                </div>             

【问题讨论】:

  • 让我们添加它。在服务器端处理它..addslashesmysqli_real_escape_string
  • 我需要jquery函数
  • 为什么我有一种可怕的感觉,认为这是一次非常失败的防止代码注入的尝试?

标签: javascript php jquery mysql ajax


【解决方案1】:
$(document).ready(function() {
    $prevent_single_double_quote = function(e) {
        var element = e;
        setTimeout(function() {
            var regexFormat = /^[a-zA-Z0-9 ]*$/;
            var text = element.val();
            if(!regexFormat.test(text)){
                //if contains single or double quotes.
                text = text.substring(0, (text.length-1));
            }
            element.val(text);
        }, 1);
    };
    $('textarea').on('change', function() {
        $prevent_single_double_quote($(this));
    });
    $('textarea').on('keyup', function() {
        $prevent_single_double_quote($(this));
    });
    $('input').on('change', function() {
        $prevent_single_double_quote($(this));
    });
    $('input').on('keyup', function() {
        $prevent_single_double_quote($(this));
    });
    $('.scle').on('keyup', function() {
        $prevent_single_double_quote($(this));
    });
}); 

希望这可能会奏效!

【讨论】:

    【解决方案2】:

    请试试这个,这个解决方法是防止输入粘贴时出现双引号和单引号。

    $(document).ready(function() {
    $prevent_single_double_quote = function(e) {
        var element = e;
        setTimeout(function() {
            var regexFormat = /^[a-zA-Z0-9\[\]\$\!\#\%\^\&\*\(\)\-\+\=\-\;\:\,\.\?\@\_\' ]*$/;
            var text = element.val();
            if(!regexFormat.test(text)){
                //if contains single or double quotes.
                text = text.substring(0, (text.length-1));
            }
            element.val(text);
        }, 1);
    };
    $('textarea').on('change', function() {
        $prevent_single_double_quote($(this));
    });
    $('textarea').on('keyup', function() {
        $prevent_single_double_quote($(this));
    });
    $('input').on('change', function() {
        $prevent_single_double_quote($(this));
    });
    $('input').on('keyup', function() {
        $prevent_single_double_quote($(this));
    });
    $('.scle').on('keyup', function() {
        $prevent_single_double_quote($(this));
    });
    $( "input" ).bind( 'paste',function()
     {
     var txtbx = $(this);
         setTimeout(function()
         { 
            //get the value of the input text
            var data= $( txtbx ).val() ;
            //replace the special characters to '' 
            var dataFull = data.replace(/((?<![\\])['"])((?:.(?!(?<![\\])\1))*.?)/gi, '');
            //set the new value of the input text without special characters
            $(txtbx).val(dataFull);
         });
    
      });
      }); 
    

    在此处查看示例:https://jsfiddle.net/Shalitha/nfovt6bz/26/

    【讨论】:

      猜你喜欢
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2013-03-24
      • 2019-07-20
      • 2023-03-25
      • 2018-05-09
      • 2011-06-02
      • 1970-01-01
      相关资源
      最近更新 更多