trigger form validation using Javascript
-
3. června 2009 7:12Hi,
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
Všechny reakce
-
3. června 2009 7:17ModerátorHi.
Copy your validation script to field OnChange Event Handler.
Truth is opened the prepared mind My blog - http://a33ik.blogspot.com -
3. června 2009 7:20Hi 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 -
3. června 2009 8:37
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/- Označen jako odpověď Andrii ButenkoMVP, Moderator 1. června 2012 15:53