How to prevent form submit if an onChange script has generated an error message?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2019 07:25 PM
My scenario is that I have an onChange client script that validates a field, and, if it fails validation, adds one or more error messages to the field. Note that I don't want to do the validation via onSubmit script, as it is more user-friendly to do the validation sooner via onChange script. Also, I don't want to clear an invalid value from the field, as it is more user friendly to let the user correct the field value (guided by the error message(s)) rather than make them start again from scratch.
But my problem is that the user can still submit the form even if the field has failed validation. This seems to me to be a ServiceNow deficiency. Surely the submit form should not be honored if there are one or more field or form error messages being displayed?
So now I am looking for a workaround, that doesn't involve duplicating code, or redoing a validation check that has already been done. Any ideas?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2019 07:33 PM
Hi
How many fields you are looking to validate? YOu can write a before insert/update business rule. Check all the validation there and if all the field values are valid then continue submit or if not valid then set error and abort the action.
You can use below function to set the abort action. so user will not be able to submit the form with invalid values
setAbortAction(true);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2019 07:40 PM
Thanks.
Just one field to validate at the moment, but there could be more.
Yes, I could do the validation via before business rule or onSubmit client script, but I want to do the validation via onChange script in order to catch the error as soon as the user makes it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2019 08:33 PM
Yes, onchange script is the solution. As you don't want to clear the value for the field and just want to display a error message, user will be able to submit the form with invalid values. So you will be required to write a before insert business rule in order to abort the insert action if submitted fields value is invalid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2023 07:46 PM
Hi @dgmdgmdgm ,
I am also trying to work on similar requirement of Onchange client script . Is it possible to restrict the form from updating in onchange script when there is field error message. Please let me know.
Thank you 🙂