locked
Issue with a Javascript-validation (triggered by onChange) RRS feed

  • Question

  • Hi,

    I'm using CRM 2016 (on-premise) and I have a issue with a Javascript-validation on a phone-field (triggering on the onChange-event):

    Here are the code:

    The validation trigger correctly when I type in a invalid value (ie "1"). But when I after this clear/empty the Field, the onChange-event are not triggered and the error text are still visible...
    Any clues on how to solve this?

    regards,

    Marius H. Enerud


    • Edited by morris80 Thursday, December 29, 2016 9:23 AM
    Wednesday, December 28, 2016 10:56 AM

Answers

  • As per the code screenshot and event you have registered, CRM behaves as expected. You need to update your code and an additional If condition to satisfy your requirement.

    Onchange will trigger if you modify(new value or clearing old value) the field. If you wantNotification to be removed then modify your if condition like below

    if(fieldValue && fieldValue.length !== 8)


    Ramanathan Rajendran MCTS - Dynamics CRM


    Saturday, December 31, 2016 5:12 AM

All replies

  • Please share your code and your error here

    Regards, Saad

    Thursday, December 29, 2016 8:47 AM
  • Sorry, missed the Pictures.. should be ok now.

    regards,

    Marius

    Thursday, December 29, 2016 9:24 AM
  • If I have understood correctly, even after you clear or empty the field the error message is shown. This because the condition will satisfy only if you enter a valid phonenumber(lenth is 8). Till then it will show notification and its an expected behavior.

    Ramanathan Rajendran MCTS - Dynamics CRM

    Friday, December 30, 2016 12:11 AM
  • Adding an additional condition for null case will clear the message. Refer Below Code:

    if(fieldValue){
    if(fieldValue.length !== 8)
    {
    Xrm.Page.ui.controls.get("telephone2").setFocus();
    Xrm.Page.getControl("telephone2").setNotification("Norwegian Local Phone Numbers must consist of 8 digits");
    return;
    }
    else{
    Xrm.Page.getControl("telephone2").clearNotification();
    }
    }
    else{
    Xrm.Page.getControl("telephone2").clearNotification();
    }

    Regards, Saad


    • Edited by Mohd Saad Friday, December 30, 2016 8:51 AM
    • Proposed as answer by Mohd Saad Friday, December 30, 2016 8:51 AM
    Friday, December 30, 2016 8:50 AM
  • Thanks for Your feedback.

    This is not a expected behaviour because there is no invalid data in the Field (the field is empty).

    The problem is that the onChange-event won't trigger when I clear the Field and lose focus (no JavaScript code will be executed).

    Friday, December 30, 2016 9:30 AM
  • I have tested the above code and when you clear the field on change event is triggered. Please apply the above code and see if the issue still exists.

    Regards, Saad

    Friday, December 30, 2016 9:36 AM
  • As per the code screenshot and event you have registered, CRM behaves as expected. You need to update your code and an additional If condition to satisfy your requirement.

    Onchange will trigger if you modify(new value or clearing old value) the field. If you wantNotification to be removed then modify your if condition like below

    if(fieldValue && fieldValue.length !== 8)


    Ramanathan Rajendran MCTS - Dynamics CRM


    Saturday, December 31, 2016 5:12 AM
  • You are correct, this is solved now.

    Thanks!

    Saturday, January 14, 2017 7:18 PM