SharePoint 2013: Enhanced / Rich TextBox Validation Using JQuery

Introduction

In this article we explored enhanced/rich text box of SharePoint 2013 validation.

I observed that the mandatory enhanced/rich text box of SharePoint 2013 doesn’t throw an error when we cut the content from it from the edit/new form and it allows us to save the form without any content in rich text box. The article describes the steps to remove this error.

error

Scenario

On the new/edit form we have enhanced/rich text which is a mandatory field. If we edit the rich text box and cut the content present inside it, the validation doesn’t work. The issue has been resolved.

Solution

Need to avoid the div tags and get data alone from multiline textbox field using the following script,

Step 1: Navigate to your SharePoint 2013 site.

Step 2: From this page select the Site Actions | Edit Page.

Edit the page, go to the "Insert" tab in the Ribbon and click the "Web Part" option. In the "Web Parts" picker area, go to the "Media and Content" category, select the "Script Editor" Web Part and press the "Add button".

Step 3: Once the Web Part is inserted into the page, you will see an "EDIT SNIPPET" link; click it. You can insert the HTML and/or JavaScript as in the following:

  1. <script type="text/javascript">  
  2.     var flag = 0;  
  3.     $(document).ready(function()   
  4.     {  
  5.         var reg1 = new RegExp("<div class=\"ExternalClass[0-9A-F]+\">[^<]*""");  
  6.         var reg2 = new RegExp("</div>$""");  
  7.         var Fieldname = $("td.ms-formlabel h3.ms-standardheader nobr:contains('Field Name')").parent().parent().parent().find("div.ms-inputBox div.ms-rtestate-write").html().replace(reg1, "").replace(reg2, "");  
  8.         var contenttext = $("td.ms-formlabel h3.ms-standardheader nobr:contains('Field Name')").parent().parent().parent().find("div.ms-inputBox div.ms-rtestate-write").text();  
  9.         if (contenttext.trim() == "")   
  10.         {  
  11.             $("td.ms-formlabel h3.ms-standardheader nobr:contains('Field Name')").parent().parent().parent().find("div.ms-inputBox div.ms-rtestate-write").html(Fieldname);  
  12.         }  
  13.     });  
  14.   
  15.     function PreSaveItem()   
  16. {  
  17.         var contenttext = $("td.ms-formlabel h3.ms-standardheader nobr:contains('Field Name)").parent().parent().parent().find("div.ms-inputBox div.ms-rtestate-write").text();  
  18.   
  19.         if (contenttext.trim() == "")  
  20.         {  
  21.   
  22.             if (flag == 0)  
  23.             {  
  24.                 $("td.ms-formlabel h3.ms-standardheader nobr:contains('Notes2')").parent().parent().parent().find("div.ms-inputBox div.ms-rtestate-write").closest("span").after("<span role='ms-formvalidation ms-csrformvalidation'><span role='alert' style='color:#bf0000!important'>You can't leave this blank.</span><br></span>");  
  25.   
  26.                 flag++;  
  27.             }  
  28.             return false;  
  29.         } else  
  30.         {  
  31.             return true;  
  32.         }  
  33.     }  
  34. </script>