Monthly Archives: June 2011

Validate a field on a Dynamics CRM form with a regular expression

Using javascript and a regexp a field can be validated easily on a CRM form. Apply the javascript below the onChange event of the wanted field. In this example the regular expression matches a swedish organizationnumber (sv: organisationsnummer).

//Check if the input value satisfies the regular expression condition
if (!ValidateOrgNr(crmForm.all.new_organisationsnr.value))
{
  //if match failed
  alert("Please enter a valid organizationnumber as XXXXXX-XXXX");
  crmForm.all.new_organisationsnr.value = "";
}

function ValidateOrgNr(nr)
{
  if(!nr.match(/^(\d{1})(\d{5})\-(\d{4})$/))
  {
    return false;
  }
  return true;
}

Advertisements

MS CRM Add a button/link to open new browser window from form

If you want to place a button on a CRM form without using the isv.config file you can do so by javascript. This examples will take a dummy attribute field and make it look and act as a button by calling a javascript function with the onclick event. Place the code below in the onLoad event of your CRM form. In this example the name och city fields are passed as search arguments to an external website.

//make a field look and act like a button
crmForm.all.stockexchange.style.textAlign = “center”;
crmForm.all.stockexchange.vAlign = “middle”;
crmForm.all.stockexchange.style.cursor = “hand”;
crmForm.all.stockexchange.style.backgroundColor = “#CADFFC”;
crmForm.all.stockexchange.style.color = “#000000”;
crmForm.all.stockexchange.style.borderColor = “#330066”;
crmForm.all.stockexchange.style.fontWeight = “bold”;

crmForm.all.stockexchange.DataValue = “Sök bolag”;
crmForm.all.stockexchange.attachEvent(“onclick”, SearchAllaBolag);


function SearchAllaBolag()
{

  //Get values from form
  if (crmForm.all.name.DataValue != null)
  {
    var what = escape(crmForm.all.name.DataValue);   
    var url = ‘
http://www.allabolag.se/?what=’ + what

    if (crmForm.all.address1_city.DataValue != null)
    {
      var where  = escape(crmForm.all.address1_city.DataValue);
      url += ‘&where=’ + where;
    }

    //open browser window
    //alert(url);
    window.open(url);
  }
  else
  {
    alert(Name is missing.’);
  }
}

Credits to http://crmstuff.blogspot.com/2008/02/create-button-on-your-form.html