Apply Client Side validation in D365 Portal / ADX Studio Portal.

We have to use JS to apply client side validation in portal. Here i am making a field mandatory in Portal using JS but this field is not mandatory in CRM. This kind of requirement comes when a field is not mandatory in CRM but you have to make a field mandatory.

Step 1: Copy the below code and paste it in Notepad.


// Apply Client Side Validation on FieldName
if (window.jQuery) {
   (function ($) {
      $(document).ready(function () {
         if (typeof (Page_Validators) == 'undefined') return;
         // Create new validator
         var newValidator = document.createElement('span');
         newValidator.style.display = "none";
         newValidator.id = "FieldNameValidator";
         newValidator.controltovalidate = "FieldName";
         newValidator.errormessage = "<a href='#FieldName_label'>FieldName is required field.</a>";
         newValidator.validationGroup = ""; // Set this if you have set ValidationGroup on the form
         newValidator.initialvalue = "";
         newValidator.evaluationfunction = function () {
            var FieldName = $("#FieldName").val();       
if (FieldName == "")       
             return false;  // return false mean apply validation            
else 
             return true;   // return true mean successful         
         };
         // Add the new validator to the page validators array:
         Page_Validators.push(newValidator);
         // Wire-up the click event handler of the validation summary link
         $("a[href='#FieldName_label']").on("click", function () { scrollToAndFocus('FieldName_label','FieldName'); });
      });
   }(window.jQuery));
}

Step 2: Now modify this code as your requirement.
  • Here Replace the FieldName with your field name.
Step 3: Now apply this code on portal's page as show in below ScreenShot.




Now Check the Portal.

To apply the Asterisk (*) Sign using custom JS : 
 $('#FieldName_label').after('<span id="spanId" style="color: red;"> *</span>');  



Click Here to know, Retrieve the data using Odata Query in D365 Portal / ADX Studio Portal.

3 comments:

Anonymous said...

I really appreciate the information shared above. It’s of great help.
Microsoft Dynamics AX Online Training

Anonymous said...

This was really helpful! Even better than the MS official website, which does not work for some reason :)

Steve Rogers said...

Nice Blog, Thanks for share an amazing information dynamics 365 customer portal