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.




  • 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();
    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");


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


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



One comment

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s