Field Validations for a form

Sai Santhosh Ch
Tera Contributor

I have a field validation for field named " K-form " MANDATORY (Must be 11 character). I have written an onchange client script for this. Error Message is showing up, but Form is being saved even if 11 character condition is not met.

As, you can see in the below picture, field is turned green and saved.   I want to restrict the form submission if condition is not met.

                          find_real_file.png

Please Help.

The code I have written for this is ,

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

          return;

    }

    //Type appropriate comment here, and begin script below

    g_form.hideFieldMsg('u_k_form', true);

if(validate(newValue) == true)

{

      return true;

}

  else{

  return false;

}

function validate(user_input)

{

  var msg = 'Must be a valid 11 characters K-Form';

 

            if (user_input.length==11)

    {

          return true;

    }

  g_form.showFieldMsg('u_k_form', msg, 'error');

  return false;

}

}

2 REPLIES 2

JusCuz
Tera Guru

Here's a page sncguru did on the subject: Stopping Record Submission - ServiceNow Guru


Loudigi
Kilo Sage

Hi Sai,



I worked on something similar only I took it a step further and forced the user to re-enter the value if they did not meet the requirements. I simply cleared the whatever they entered if it wasn't the right amount of characters. So after the line of code for the error message I inserted g_form.setValue('field_name_here', '')


Basically resetting the field.