Hi,
here's whole script, i think there is a problem with Xrm.Page.getControl(keypressphone).getValue().replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
control = ["telephone1", ''mobilephone'']
function PhoneMasking(control) {
if (control == undefined || control == null) { return; }
if (typeof (control) == "string") {
callFormatPhoneNumber(control);
}
else if (typeof (control) == "object") {
for (var iControl = 0; iControl < control.length; iControl++) {
if (Xrm.Page.getControl(control[iControl]) != null) {
callFormatPhoneNumber(control[iControl]);
}
}
}
}
callFormatPhoneNumber = function (control) {
var phone = Xrm.Page.getAttribute(control);
//To get field label name
var label = Xrm.Page.getControl(control).getLabel();
//Formats the phone number to US Format(123-456-7890)
FormatPhoneNumber(phone, label, control);
//Formats the phone number to US format as user types
keypressmasking(control, label);
}
function FormatPhoneNumber(number, label, control)
{
var BusinessAllNumeric = number;
var labelValue = label;
//Check the field exists and is not null
if (typeof (number) != "undefined" && number.getValue() != null) {
if (labelValue) {
BusinessAllNumeric = number.getValue().replace(/[^0-9]/g, "");
if (BusinessAllNumeric.length == 10 || BusinessAllNumeric.length == 0) {
FormattedBusinessNumber = BusinessAllNumeric.substr(0, 3) + "-" + BusinessAllNumeric.substr(3, 3) + "-" + BusinessAllNumeric.substr(6, 4);
number.setValue(FormattedBusinessNumber);
// To clear form notification
Xrm.Page.ui.clearFormNotification(labelValue);
// To clear field notification
Xrm.Page.getControl(control).clearNotification();
}
else {
// To set form notification
Xrm.Page.ui.setFormNotification(labelValue + " must contain 10 digits.", "WARNING", labelValue);
// To set field notiffication
Xrm.Page.getControl(control).setNotification(labelValue + " must contain 10 digits.");
}
}
}
}
function keypressmasking(keypressphone, label)
{
var notificationid = label;
// check the field exists
if (typeof (keypressphone) != "undefined")
{
//To show form notification on key press events based on conditions
Xrm.Page.getControl(keypressphone).addOnKeyPress(function () {
if (notificationid) {
var phoneNo = Xrm.Page.getControl(keypressphone).getValue();
phoneNo = phoneNo.replace("-", "").replace("-","");
if (phoneNo.length == 10 || phoneNo.length == 0) {
var formattedPhone = phoneNo.replace(/(\d{3})(\d{3})(\d{4})/, '$1-$2-$3');
Xrm.Page.getAttribute(keypressphone).setValue(formattedPhone);
Xrm.Page.ui.clearFormNotification(notificationid);
Xrm.Page.getControl(keypressphone).clearNotification();
}
else {
Xrm.Page.ui.setFormNotification(notificationid + " must contain 10 digits.", "WARNING", notificationid);
Xrm.Page.getControl(keypressphone).setNotification(notificationid + " must contain 10 digits.");
}
}
});
}
}
Thanks,
Hardik Chauhan