我有一个<p:editor />
属性为maxlength,但是用户可以粘贴大于maxlength的文本。
这里是所有我要检查的内容,即粘贴的时间是否长于最大长度;如果是真的,我想剪切粘贴的文本的子字符串(0,max)。
document.getElementById("editor").onpaste = function(e){
var max = document.getElementById("editor").getAttribute("maxlength");
e.clipboardData.getData('text/plain').slice(0, max);
};
$('.editor').live('paste', function(){
console.log('function');
if ($('.editor').val().length > 140) {
console.log('errrrrrrrrrrrorrrrrrrrrrrrrrrrr');
$('.editor').val($('#editor').val().substr(0,10));
}
});
var max = 140;
$('#shopCustomizationForm\\:rongnk').find('iframe').contents().find("body").on('keyup', function(e) {
ilength = $('#shopCustomizationForm\\:rongnk').find('iframe').contents().find("body").text().length;
$('#shopCustomizationForm\\:txt').html('Remain:' + (max - ilength));
});
XHTML:
<p:editor widgetVar="CEMessage_Widget"
id="editor"
value="#{dcShopCustomizationManagedBean.customBean.customText}"
controls="bold italic underline strikethrough color size alignleft center alignright justify bullets numbering"
maxlength="140" />