locked
trigger form validation using Javascript RRS feed

  • Question

  • Hi,
    I have a requirement to manually trigger form validation to check if required fields are populated.

    validation is automatically triggered when trying to save; i would like to manually or in my scrip trigger the validation. Is there a way to do this?

    Thanks
    Wednesday, June 3, 2009 7:12 AM

Answers

  • You can simulate the save without saving the form but you need a trigger like an onchange event handler attached to a field.

    If the validation does not succeed then the save handler is not called. If it does you need to abort saving since it was not triggered by the user.

     

    This is just an idea you can try.

     

    e.g.

     

    window.CanSave = true;

     

    function OnCrmPageLoad()
    {
        /* attach onchange to fields that need to simulate save */

       AttachFieldsThatNeedReqValidation();

        /* refuse save in the OnCrmPageSaveStub */

       crmForm.attachEvent( “onsave” , OnCrmPageSaveStub );
    }

     

    function AttachFieldsThatNeedReqValidation ()
    {

        crmForm.<some field1 id>.attachEvent(“onchange” , OnSimulateSaveForValidation );

        crmForm.<some field2 id>.attachEvent(“onchange” , OnSimulateSaveForValidation );

        crmForm.<some field3 id>.attachEvent(“onchange” , OnSimulateSaveForValidation );
    }

     

    function OnSimulateSaveForValidation()
    {
      window. CanSave = false;

      crmForm.Save(); /* Triggers OnCrmPageSaveStub */
    }

     

    function OnCrmPageSaveStub ()
    {
          window. CanSave = true; /* revert for consequent validations */

          return (event.returnValue = false);
    }

     

    OnCrmPageLoad();

     

    Another option, less complicated,  is to do the same validation for each field yourself.

     


    Blog: http://mscrm4ever.blogspot.com/ * Website: http://gicrm.upsite.co.il/
    Wednesday, June 3, 2009 8:37 AM

All replies

  • Hi.

    Copy your validation script to field OnChange Event Handler.
    Truth is opened the prepared mind My blog - http://a33ik.blogspot.com
    Wednesday, June 3, 2009 7:17 AM
    Moderator
  • Hi Andriy,
    Thanks for the reply. My focus now is just on the regular validation of the required fields that's triggered when we try to save. i dont want to add a custom validation in my scripts. is there a way to trigger this form validation?

    Thanks
    Wednesday, June 3, 2009 7:20 AM
  • You can simulate the save without saving the form but you need a trigger like an onchange event handler attached to a field.

    If the validation does not succeed then the save handler is not called. If it does you need to abort saving since it was not triggered by the user.

     

    This is just an idea you can try.

     

    e.g.

     

    window.CanSave = true;

     

    function OnCrmPageLoad()
    {
        /* attach onchange to fields that need to simulate save */

       AttachFieldsThatNeedReqValidation();

        /* refuse save in the OnCrmPageSaveStub */

       crmForm.attachEvent( “onsave” , OnCrmPageSaveStub );
    }

     

    function AttachFieldsThatNeedReqValidation ()
    {

        crmForm.<some field1 id>.attachEvent(“onchange” , OnSimulateSaveForValidation );

        crmForm.<some field2 id>.attachEvent(“onchange” , OnSimulateSaveForValidation );

        crmForm.<some field3 id>.attachEvent(“onchange” , OnSimulateSaveForValidation );
    }

     

    function OnSimulateSaveForValidation()
    {
      window. CanSave = false;

      crmForm.Save(); /* Triggers OnCrmPageSaveStub */
    }

     

    function OnCrmPageSaveStub ()
    {
          window. CanSave = true; /* revert for consequent validations */

          return (event.returnValue = false);
    }

     

    OnCrmPageLoad();

     

    Another option, less complicated,  is to do the same validation for each field yourself.

     


    Blog: http://mscrm4ever.blogspot.com/ * Website: http://gicrm.upsite.co.il/
    Wednesday, June 3, 2009 8:37 AM