Search code examples
javascriptdynamics-crm-online

Mob. number validation in js CRM2016


I'm using below code for accepting +xxxxxxxxx format but I'm getting error like Cannot read property 'match' of undefined at phonenumber Can you plz suggest me where I'm committing mistake ;

Below code:

function phonenumber() {
    var inputtxt = Xrm.Page.data.entity.attributes.get("new_phonenumber").getValue();
    var phoneno = /^+?([0-9]{2}))?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
    if (inputtxt.match(phoneno)) {
        return true;
        Xrm.Page.getControl("new_phonenumber").clearNotification();
    } else {
        Xrm.Page.getControl("new_phonenumber").setNotification("Format error");
        return false;
    }
}

I'm refreing this :Validate phone number using javascript


Solution

  • You need to make sure inputtxt is not null (as in when the field is empty).

    Something like

    if (inputtxt && inputtxt.match(phoneno))
    

    Should do the trick.