Sunday, January 30, 2011

JavaScript - Date of Birth Validation

As a requirement for a project, I had to make sure that the applicant was at least 18 years of age. The code snippet below checks that the person is of age and (if he or she is underage) displays an alert pop-up. This code should be placed in the onSave event and will effectively prevent the CRM form from saving if the person is less than 18 years old:

if (crmForm.all.gov_birthdate.DataValue != null)  
  {  
           var maxDate = new Date(); //get today's date 
           maxDate.setFullYear(maxDate.getFullYear() - 18);

           if (maxDate < crmForm.all.gov_birthdate.DataValue)   //under 18  
            {

                 alert('An applicant must be at least 18 years old.');
                 event.returnValue = false;
                 return false;

            }


  }

Saturday, January 29, 2011

JavaScript - Get Object Type Code for any Entity

I needed to find the specific Entity code based on its location with respect to the CRM tenant. The code below should be placed in the onLoad event of an entity. When the form is opened, the Entity code will display in an alert pop-up:

//put in onLoad event of entity
     alert('Object Type Code: ' + crmForm.ObjectTypeCode);

Friday, January 28, 2011