您可能已经知道SharePoint中有一个InputFormTextBox控件,设置其属性RichText为True,呈现出来的就是一个RichText编辑器。有时候需要验证这个控件是否输入字符,通常我们会想到RequiredFieldValidator控件或InputFormRequiredFieldValidator控件来做验证,试了几次,这种解决方案没有达到真正的效果。于是Google了一把,找到一篇博客Validating the SharePoint InputFormTextBox / RichText Editor using JavaScript,可以用JavaScript去验证,于是决定用CustomValidator控件的ClientValidationFunction,结果成功解决问题。
动态创建CustomValidator:
CustomValidator cv = new CustomValidator(); cv.ID = "cvTest"; cv.ErrorMessage = "The field is required."; cv.ControlToValidate = "txtTest"; cv.Display = ValidatorDisplay.Dynamic; cv.ClientValidationFunction = "RichTextClientValidate"; cv.ValidateEmptyText = true; this.Controls.Add(cv);
客户端验证JavaScript:
function RichTextClientValidate(source, arguments) { var text = RTE_GetRichEditTextOnly(source.controltovalidate); if (text == '') { arguments.IsValid = false; } else { arguments.IsValid = true; } }