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 09:14 PM
Yes, that is basically what I am doing in the onChange script:
g_form.showFieldMsg("field_name", "Hello World", "error");
So an API similar to this:
g_form.getError("field_name");
would be ideal. But there doesn't appear to be anything like this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2019 09:15 PM
Yes, that is basically what I am doing in the onChange script:
g_form.showFieldMsg("field_name", "Hello World", "error")
So an API similar to this:
g_form.getError("field_name")
would be ideal. But there doesn't appear to be anything like this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2019 09:45 PM
seem there no way to get Error Msg, how about you create 2 catalog script, one if Onchange it show Error MSG, one OnSubmit if the validation is false, return false the onsubmit? I think it is possible right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-09-2021 12:18 AM
Do we have a solution for this requirement now?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2023 05:27 AM
Hi @dgmdgmdgm,
I would say you can set Label field with suffix when error and then verify Label contains that suffix to check if field in form contains any Field error msgs
- onChange client script invoke function from Script Include to pass parameters
- Script Include will return answer after validating parameters
- onChange client script will set label values and field messages based on answer
var answer = response.responseXML.documentElement.getAttribute("answer");if (answer == 'Error') {
g_form.setLabelOf('assignment_group', 'Assignment group?');
g_form.showFieldMsg('assignment_group', "Assignment Group warning msg", 'error');
} - onSubmit client script will check if Label of field contains suffix
var groupLabel = g_form.getLabelOf('assignment_group');
if (groupLabel == 'Assignment group?') {
return false;
}
If you find my comment useful, mark it as correct or helpful.
Regards,
Deepak Ramar