Back to Basics # 72: Limit Special Characters Using a Webresource in Dynamics CRM

Recently we got a requirement to restrict user to enter special characters.

Step 1 : Use the below method for restricting special characters

function checkForSpecialCharacters(executionContext) {
    var formContext = executionContext.getFormContext();
    var detailfieldcontrol=formContext.getControl("cmn_details");
    var detailfield = formContext.getAttribute("cmn_details").getValue();
    if (detailfield === null || detailfield ==='') return;
    var specialChars = "!@$%";
   for (var i = 0; i < detailfield .length; i++)
       {
         if (specialChars.indexOf(detailfield .charAt(i)) != -1)
        {
           // console.log('checkForSpecialCharacters-special characters');
              return false;
        }
      }
}

Step 2 : Call the above function in the form load of webresource javascript file related to the entity example here I have called the above piece of code in form load event of entity in the below way

var spchracters=true;
spchracters=checkForSpecialCharacters(executionContext);
 if(!spchracters && spchracters!=undefined)
            {
                detailfieldcontrol.setNotification("Special Characters !@$% are Not Allowed", "DescriptionErrorNotifications");
            }
   else{
  detailfieldcontrol.clearNotification("DescriptionErrorNotifications");
}

Note : Step 2 code and Step 1 code should be written in webresource js file and register that js file in the entity respective field on change event. if you want to know about how to register please do refer my earlier blogs.

Conclusion: In this way , one can easily perform client side validations


Discover more from Common Man Tips for Power Platform, Dynamics CRM,Azure

Subscribe to get the latest posts sent to your email.

Leave a comment