Tuesday, January 31, 2012

Validation on the field

here some simple code to validate the field of the form in crm


main  phone

var e = /^(\+91[\-\s]?)?[123456789]\d{9}$/;


if(crmForm.all.telephone1.value.search(e) == -1)
{alert('number should be proper');
crmForm.all.telephone1.value = '';                  //to make field blank.
crmForm.all.telephone1.SetFocus();               //to set the focus on particular field.
event.returnValue=false;
}

fax

var e = /^[+]\d{12}$/;


if(crmForm.all.fax.value.search(e) == -1)
{alert('number should be proper');
crmForm.all.fax.value = '';
crmForm.all.fax.SetFocus();
event.returnValue=false;
}


city

var e = /^[a-zA-Z\s]+$/;
if (crmForm.all.address1_city.value.search(e)==-1)
{
  alert("Only alphbets or space allowed");
crmForm.all.address1_city.value = '';
crmForm.all.address1_city.SetFocus();
event.returnValue=false;
}


zip postal code

var e =/^\d{6}$/;


if(crmForm.all.address1_postalcode.value.search(e) == -1)
{alert('number should be proper');
crmForm.all.address1_postalcode.value = '';
crmForm.all.address1_postalcode.SetFocus();
event.returnValue=false;}

first name

var e = /^[a-zA-Z\s]+$/;
if (crmForm.all.firstname.value.search(e)==-1)
{
  alert("Only alphbets or space allowed");
 crmForm.all.firstname.value = '';
crmForm.all.firstname.SetFocus();
event.returnValue=false;
}

state/province

var e = /^[a-zA-Z\s]+$/;
if (crmForm.all.address1_stateorprovince.value.search(e)==-1)
{
  alert("Only alphbets or space allowed");
crmForm.all.address1_stateorprovince.value = '';
crmForm.all.address1_stateorprovince.SetFocus();
event.returnValue=false;
}

file browse button


very easy to create a file browse button.


follow the below step.

1. add one text attribute named new_filepath which store the path of the file that you have browse.
2. then add the following code in the OnLoad event of the form.


inputF = document.createElement('input');
inputF.type = 'file';
inputF.onchange = inputF_onChange;


tbl = crmForm.all.tab0.getElementsByTagName('table');
if (tbl && tbl[0])
{
    tbl[0].parentNode.insertBefore(inputF, tbl[0]);
}


function  inputF_onChange()
{
if (inputF && inputF.value)
crmForm.all.new_filepath.DataValue = inputF.value;
}


enjoy it.

Checkbox in crm 4.0

yes,it is possible to create checkbox in crm 4.0

the step which you have to follow is...


  • create one picklist named new_picklist(you can keep different name also but then you have to change the name in the following code).
  • then create another text attribute to store the value of checkbox.
  • then put the following code in the Onload event of the form.



var PL = crmForm.all.new_picklist;
var PLV = crmForm.all.new_picklistvalue;

if( PL != null && PLV != null )
{
  PL.style.display = "none";
  PLV.style.display = "none";

  // Create a DIV container
  var addDiv = document.createElement("<div style='overflow-y:auto; height:80px; border:1px #6699cc solid; background-color:#ffffff;' />');
  PL.parentNode.appendChild(addDiv);

  // Initialise checkbox controls
  for( var i = 1; i < PL.options.length; i++ )
  {
    var pOption = PL.options[i];
    if( !IsChecked( pOption.text ) )
      var addInput = document.createElement("<input type='checkbox' style='border:none; width:25px; align:left;' />' );
    else
      var addInput = document.createElement("<input type='checkbox' checked='checked' style='border:none; width:25px; align:left;' />' );
 
    var addLabel = document.createElement( "<label />");
    addLabel.innerText = pOption.text;
 
    var addBr = document.createElement( "
"); //it's a 'br' flag
 
    PL.nextSibling.appendChild(addInput);
    PL.nextSibling.appendChild(addLabel);
    PL.nextSibling.appendChild(addBr);
  }
 
  // Check if it is selected
  function IsChecked( pText )
  {
    if(PLV.value != "")
    {
      var PLVT = PLV.value.split("||");
      for( var i = 0; i < PLVT.length; i++ )
      {
        if( PLVT[i] == pText )
          return true;
      }
    }
    return false;
  }
 
  // Save the selected text, this filed can also be used in Advanced Find
  crmForm.attachEvent( "onsave" , OnSave);
  function OnSave()
  {
    PLV.value = "";
    var getInput = PL.nextSibling.getElementsByTagName("input");
 
    for( var i = 0; i < getInput.length; i++ )
    {
      if( getInput[i].checked)
      {
        PLV.value += getInput[i].nextSibling.innerText + "||";
      }
    }
  }
}