Showing posts with label Phone number validation. Show all posts
Showing posts with label Phone number validation. Show all posts

Friday, December 23, 2016

Validation of Telephone Number using Javascript

function telephone_Onchange(fieldname) {
    var targetattribute = Xrm.Page.getAttribute(fieldname);
    var phonenumber = targetattribute.getValue();
    var targetcontrol = Xrm.Page.getControl(fieldname);
    if (phonenumber == null) {
        targetcontrol.clearNotification();
        return;
    }
    if (phonenumber.length == 0) {
        targetcontrol.clearNotification();
        return;
    }
    phonenumber = phonenumber.replace(/\+/g, "");
    phonenumber = phonenumber.replace(/\(/g, "");
    phonenumber = phonenumber.replace(/\)/g, "");
    phonenumber = phonenumber.replace(/\-/g, "");
    phonenumber = phonenumber.replace(/\s/g, "");
    phonenumber = phonenumber.replace(/\./g, "");
    if (isNaN(phonenumber) || phonenumber.length < 10 || phonenumber.length > 12) {
        targetcontrol.setNotification("Telephone number not validIt ");
        return;
    }
    targetcontrol.clearNotification();

}