Home > CRM 2011, JScript > Using Regular Expression and JScript to validate phone no format

Using Regular Expression and JScript to validate phone no format

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 🙂

Advertisement
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: