使用RequiredFieldValidator控件验证FCKeditor是否填写内容的时候会遇到一个问题,
填写了内容之后第一次提交还会提示没有填写内容,这是由于FCKEditor的编辑框采用的是Iframe,每次提交的时候首先要将Iframe的内容复制到文本框内,再提交,但是验证控件验证的是实际的文本框,在没触发提交时,这个内容文本框的内容实际上是空的。
换上自定义验证控件CustomValidator
设置ClientValidationFunction="checkFckeditorContent";
利用FCKeditor提供的Javascript API可以解决这个问题
http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/JavaScript_API#Events
在页面上加入以下脚本
<script type="text/javascript">
//<![CDATA[
var fckeditorContentLength = 0;
function checkFckeditorContent(source, arguments)
{
arguments.IsValid = (fckeditorContentLength > 0);
}
function FCKeditor_OnComplete(editorInstance)
{
editorInstance.Events.AttachEvent('OnBlur', FCKeditor_OnBlur); //附加失去焦点的事件
}
function FCKeditor_OnBlur(editorInstance)
{
fckeditorContentLength = editorInstance.GetXHTML(true).length;
//在FCKeditor失去焦点时,如果内容不为空则隐藏验证提示
if(fckeditorContentLength > 0){
document.getElementById("<%= this.自定义验证控件.ClientID %>").style.display = "none";
}
}
//]]>
</script>
问题到此就解决了