To show validation on a field using Regex Expression

In this blog, I am going to show how we can display a validation on the field if the entered text is not in the required format

Let’s consider a use case,

We have a field name ‘DMV Initial License’ which is a single line of text field. When text is entered the Date format for this field should be MM/YYYY

Let’s have a look at JavaScript where the magic is making this possible.

Step 1: Regex Expression for Date in the required format MM/YYYY – date_regex = /^(0[1-9]|1[0-2])\/[/]?([0-9]{4})$/

Step 2: Below is the entire code of JS

checkDateFormat: function (executionContext, fieldname) {
  debugger;
  var formContext = executionContext.getFormContext();
  var fieldAttribute = formContext.getAttribute(fieldname);
  if (fieldAttribute) {
   var fieldAttributeValue = fieldAttribute.getValue();
   const date_regex = /^(0[1-9]|1[0-2])\/[/]?([0-9]{4})$/;
   if (fieldAttributeValue) {
    if (fieldAttributeValue.match(date_regex) != null) {
     formContext.getControl(fieldname).clearNotification();
    }
    else {
     formContext.getControl(fieldname).setNotification("Invalid Date format - Set as MM/YYYY");
    }
   }
  }
 },

For Table/Entity Main Form customization, select ‘Form Properties’ to include JavaScript function as below,

On Change of field ‘ DMV Initial License ’: oCustomerFormCustomization.checkDateFormat

Note: Pass the execution context for calling the JS function.

Hope this helps!!


Share Story :

Secured By miniOrange