Wednesday, July 4, 2012

Microsoft Dynamics CRM 2011 Validate required form javascript

Microsoft Dynamics CRM 2011 Validate required form javascript.

This code checks if form is valid for saving, by going over all required attributes and checking if it contains value.


function IsFormValidForSaving(){
var valid = true;
Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
   if (attribute.getRequiredLevel() == "required") {
      if(attribute.getValue() == null){
          if(valid){
              var control = attribute.controls.get(0);
              alert(control.getLabel() + " is missing a value");
              control.setFocus();
          }
          valid = false; 
      }
   }
});
return valid;
}
Another option is using crm system function but then it won't be supported.

2 comments: