Advertisements

Archive

Archive for February 27, 2012

Using Regular Expression and JScript to validate phone no format

February 27, 2012 Leave a comment

Hi,

I got a requirement to validate a CRM field with “Indian Telephone Number” format (i.e., 040-1234567).

I opted to use regular expression and Jscript for format validation.

Below is the JScript function

function businessPhoneOnChange() {

var telephone = Xrm.Page.data.entity.attributes.get(“telephone1”);

// Regular expression with Indian Telephone format

var regExp= /^[0-9]\d{2,4}-\d{6,8}$/;

if (telephone) {

var phoneNumber = telephone.getValue();

if (phoneNumber.search(regExp) == -1) {

alert(“Invalid Phone number format”);

// Clear the field value

phoneNumber.setValue(“”);

}

}

}

  • Add a webresource with this function to CRM
  • Register “onchange” event of field with the above method
  • Save & Publish

Hope it helps 🙂

Advertisements