CKEditor无法验证的解决方案(js验证+jQuery Validate验证)
最近项目的前端使用了jQuery,表单的前端验证用的是jQueryValidate,用起来很简单方便,一直都很满意的。
前段时间,根据需求为表单中的textarea类型的元素加上了html富文本编辑器,用的是CKEditor,功能强大,定制方便,也很满意。
不过用CKEditor增强过的textarea元素,这个字段要求是非空的,在jQueryValidate总是验证不通过,原因就是在CKEditor编辑器填写了内容之后,编辑器并不是立即把内容更新到原来的textarea元素中的,我没仔细看源代码,试过一种情况就是每一次提交不通过,第二次提交就可以通过的,貌似编辑器是在submit事件之前把编辑器的内容更新到textarea中的(这个是猜测,不知道对不对,我对jQuery和CKEditor都不太熟悉,算是拿来就用,有问题就放狗的那种)。
于是在网上找到了解决问题的代码,代码不是我写的,我只是记录一下我遇到的问题,代码非原创。原理就是当编辑器更新了内容之后,立即把内容更新到textarea元素。
CKEDITOR.instances["page_content"].on("instanceReady",function()
{
//setkeyupevent
this.document.on("keyup",updateTextArea);
//andpasteevent
this.document.on("paste",updateTextArea);
});
functionupdateTextArea()
{
CKEDITOR.tools.setTimeout(function()
{
$("#page_content").val(CKEDITOR.instances.page_content.getData());
$("#page_content").trigger('keyup');
},0);
}
目前一切使用正常,算是解决了一个让我头痛的问题。
另一种解决思路:
CKEditor编辑器是增强过的textarea元素,在填写了内容之后,编辑器并不立即把内容更新到原来的textarea元素中的,而是等到submit事件之前把编辑器的内容更新到textarea中.
因此,普通的js验证或是jqueryvalidate验证都获取不到编辑器的值.)
1.js验证
获取CKEditor编辑器的值其实很容易,其值就是CKEDITOR.instances.mckeditor.getData(),实例代码如下:
<scriptlanguage="javascript"type="text/javascript">
functioncheckForm()
{
varf=document.form1;
vartopicHeading=f.tbTopicHeading.value;
topicHeading=topicHeading.replace(/^\s+/g,"");
topicHeading=topicHeading.replace(/\s+$/g,"");
if(topicHeading=="")
{
alert("请输入发表话题的标题.");
f.tbTopicHeading.focus();
returnfalse;
}
if(topicHeading.length>50);
{
alert("话题的主题长度必须在50字符以内.");
f.tbTopicHeading.focus();
returnfalse;
}
vartopicContent=CKEDITOR.instances.mckeditor.getData();
topicContent=topicContent.replace(/^\s+/g,"");
topicContent=topicContent.replace(/\s+$/g,"");
if(topicContent=="")
{
alert("请填写话题内容.");
f.mckeditor.focus();
returnfalse;
}
if(topicContent.length>4000)
{
alert("话题内容的长度必须在4000字符以内.");
f.mckeditor.focus();
returnfalse;
}
}
</script>
其中,mckeditor为编辑器的textarea的id和name.
ASP.NET中也是一样:
<asp:TextBoxID="mckeditor"runat="server"TextMode="MultiLine"Width="94%"Height="400px"CssClass="ckeditor"></asp:TextBox>
2.jQueryValidate验证
jquery的验证模式不能直接使用CKEDITOR.instances.mckeditor.getData()这个值.
它是使用如下形式来提交验证:
functionInitRules(){
opts={
rules:
{
tbTopicHeading:{
required:true,
maxlength:50
},
mckeditor:{
required:true,
maxlength:4000
}
},
messages:
{
tbTopicHeading:{
required:"请输入发表话题的标题.",
maxlength:jQuery.format("话题的主题长度必须在50字符以内.")
},
mckeditor:{
required:"请填写话题内容.",
maxlength:jQuery.format("话题内容的长度必须在4000字符以内.")
}
}
}
}
其中mckeditor为控件id,不仅有取值的作用,还有提示信息定位的作用.
因此,可以在页面加载时,加入实例化编辑器代码,实现编辑器更新了内容之后,立即把内容更新到textarea元素。
代码如下:
<scripttype="text/javascript">
//<![CDATA[
CKEDITOR.instances["mckeditor"].on("instanceReady",function()
{
//setkeyupevent
this.document.on("keyup",updateTextArea);
//andpasteevent
this.document.on("paste",updateTextArea);
});
functionupdateTextArea()
{
CKEDITOR.tools.setTimeout(function()
{
$("#mckeditor").val(CKEDITOR.instances.mckeditor.getData());
$("#mckeditor").trigger('keyup');
},0);
}
//]]>
</script>
此段代码放在编辑器控件之下即可.完整实例如下:
<asp:TextBoxID="mckeditor"runat="server"TextMode="MultiLine"Width="98%"Height="400px"CssClass="ckeditor"></asp:TextBox>
<scripttype="text/javascript">
//<![CDATA[
CKEDITOR.replace('<%=mckeditor.ClientID%>',//mckeditor.ClientID为TextBoxmckeditor生成的对应客户端看到的id
{
skin:'kama',//设置皮肤
enterMode:Number(2),//设置enter键的输入1.<p>2为<br/>3为<div>
shiftEnterMode:Number(1),//设置shiftenter的输入
disableNativeSpellChecker:false,
scayt_autoStartup:false,
toolbar_Full:[
['Source','-','Save','NewPage','Preview','-'],
['Cut','Copy','Paste','PasteText','PasteFromWord','-'],
['Undo','Redo','-','Find','Replace','-','SelectAll','RemoveFormat'],
['NumberedList','BulletedList','-','Outdent','Indent'],
['JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
['Link','Unlink','Anchor'],
['Image','Table','HorizontalRule'],['Subscript','Superscript'],
'/',
['Bold','Italic','Underline'],
['TextColor','BGColor'],
['Styles','Format','Font','FontSize']
],
//filebrowserBrowseUrl:'<%=ResolveUrl("~/ckfinder/ckfinder.html")%>',//启用浏览功能,正式使用场合可以关闭,只允许用户上传
//filebrowserImageBrowseUrl:'<%=ResolveUrl("~/ckfinder/ckfinder.html?Type=Images")%>',
//filebrowserImageUploadUrl:'<%=ResolveUrl("~/ckfinder/core/connector/aspx/connector.aspx?command=QuickUpload&type=Images")%>'如果使用ckfinder就不要屏蔽
//自定义的上传
filebrowserImageUploadUrl:'<%=ResolveUrl("~/fileupload/fileupload.aspx?command=QuickUpload&type=Images")%>'
});
CKEDITOR.instances["mckeditor"].on("instanceReady",function()
{
//setkeyupevent
this.document.on("keyup",updateTextArea);
//andpasteevent
this.document.on("paste",updateTextArea);
});
functionupdateTextArea()
{
CKEDITOR.tools.setTimeout(function()
{
$("#mckeditor").val(CKEDITOR.instances.mckeditor.getData());
$("#mckeditor").trigger('keyup');
},0);
}
//]]>
</script>
以上就是解决CKEditor无法验证的两种方案,相信大家和小编一样都有所收获,谢谢大家的阅读。