2015-12-18 69 views
2

如何防止輸入和粘貼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>    
+0

讓我們使用添加它。處理它在服務器端..'addslashes或'mysqli_real_escape_string' – Rayon

+0

我需要jquery函數 –

+1

爲什麼我有一個可怕的感覺,這是一個**非常破**試圖防止代碼注入? – eggyal

回答

0
$(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)); 
    }); 
}); 

希望這可能會奏效!