How to prevent form submit if an onChange script has generated an error message?

dgmdgmdgm
Kilo Contributor

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?

14 REPLIES 14

dgmdgmdgm
Kilo Contributor

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.

dgmdgmdgm
Kilo Contributor

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.

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?

Isac Newton
Tera Contributor

Do we have a solution for this requirement now?

Deepak Ramar1
Tera Expert

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

  1. onChange client script invoke function from Script Include to pass parameters
  2. Script Include will return answer after validating parameters
  3. 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');
                  }

  4. 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