Validating year field in Dynamics 365 SubGrid using JavaScript

  • To add validation to year field we need to first retrieve that value from the page using XRM object.
  • Add a JavaScript file to the Entity form.

1

2

3

  • Add the below code to the JavaScript file
//Regex to check year validity
function IsYear(year) {
  // alert("entered IsYear method");
    return /^\d{4}$/.test(year);
}

function subgridYearfieldCheck() {
    var gridControl = Xrm.Page.getControl("EducationalqualificationSubGrid");
    var entity = gridControl.getGrid().getSelectedRows().get(0).getData().getEntity();
    var id = entity.getId();
    //alert(id);
    try {
        //alert("entered try");
        var year = entity.getAttributes().get("yearofpassing").getValue();
      //  alert(year);
    } catch (e) {
       // alert("entered catch");
        //alert("Error: " + e);
    }
    var check = IsYear(year);
    if (!check) {
        //alert("entered setvalue");
        entity.getAttributes().get("yearofpassing").setValue("");
    }
}

 

  • Add the function “subgridYearFieldCheck” on field change in the SubGrid events.

4

  • On entering an invalid year, the field gives an error.

5

6

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s