Wednesday 30 April 2014

Phone number validation in crm 2011

The Below snippet is essential for Validating Phone no in CRM2011. Add the JavaScript as Web Resource in CRM. Add Web Resource as On-Change Event of a particular Phone no field. Also check the option of Pass Execution Context as first parameter in handler properties.

function validatePhone(context)
{

var phone =context.getEventSource().getValue();
var sTmp = phone.replace(/[^0-9]/g, "");
phoneRegex = /^\d{10}$/;

if( !sTmp.match( phoneRegex ) )
   {
   event.returnValue = false;
   alert("Phone must contain 10 numbers.") ;
   }
else
  {
   var sTmpClean =  "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
   context.getEventSource().setValue(sTmpClean);
  }
}


 Here the Phone no is validated that it contains 10 Characters and all are numeric and later placed in a Pr-defined format which is "(xxx)xxx-xxxx".

No comments:

Post a Comment