如何防止在cleditor中输入和粘贴单引号和双引号?
Posted
技术标签:
【中文标题】如何防止在cleditor中输入和粘贴单引号和双引号?【英文标题】:how to prevent entering and pasting single and double quotes inside cleditor? 【发布时间】:2016-03-24 22:14:50 【问题描述】:
<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>
【问题讨论】:
让我们添加它。在服务器端处理它..addslashes
或mysqli_real_escape_string
我需要jquery函数
为什么我有一种可怕的感觉,认为这是一次非常失败的防止代码注入的尝试?
【参考方案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/
【讨论】:
以上是关于如何防止在cleditor中输入和粘贴单引号和双引号?的主要内容,如果未能解决你的问题,请参考以下文章