首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何防止在cleditor中输入和粘贴单引号和双引号?

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

Stack Overflow用户
提问于 2015-12-18 16:45:47
回答 2查看 1.1K关注 0票数 3

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

代码语言:javascript
复制
<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>             
EN

回答 2

Stack Overflow用户

发布于 2015-12-18 18:14:34

代码语言:javascript
复制
$(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));
    });
}); 

希望这能奏效!

票数 0
EN

Stack Overflow用户

发布于 2020-01-04 13:20:46

请尝试这个,这个解决方案是为了防止在输入和粘贴时使用双引号和单引号。

代码语言:javascript
复制
$(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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34351624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档