onChange Client script

SubbarayappS
Giga Contributor

How to throw an alert message when a filed (Ex: test)  is empty??

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   if (test == '') {
      alert('test cannot be empty!');
   }
}

Why this code is not working??
4 REPLIES 4

KanteS
Tera Expert

Hi @SubbarayappS 

 

Your script is not working for two main reasons:

  1. test is a field name, not a JavaScript variable
    In client scripts, form fields must be accessed using g_form.getValue('field_name').

  2. You are returning early when the field is empty
    The condition newValue === '' causes the function to exit before your validation runs.  

 

Here is the the working code:

 

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

if (isLoading) {

return;

}

var testValue = g_form.getValue('test');

if (!testValue) {

alert('Test cannot be empty!');

}

}

 

or 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}

if (!newValue) {
 alert('test cannot be empty!'); 
}
}

 

If this answers your question, please consider marking it as the solution and clicking Helpful.

 

Thank you

Roshnee Dash
Kilo Sage

Hi @SubbarayappS ,

Here’s Can you please confirm your requirement and specify on which field change this client script is triggered?

1. test is NOT a variable

In ServiceNow client scripts, form fields cannot be accessed directly by their name. You must use g_form.getValue() to retrieve a field’s value.

 
if (test == '')

This condition will always fail because test is undefined.

 

2. You are exiting the function when the field is empty

This condition causes the script to return immediately:

 
if (isLoading || newValue === '') { return; }

So your validation logic never runs when the field is cleared.


If the onChange client script is on the test field itself

Option 1: Show alert when the user clears the field (most common use case)

 
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading) { return; } if (newValue === '') { alert('Test cannot be empty!'); } }

 

If the onChange client script is on another field, but you want to validate test

Option 2: Check the test field value when a different field changes

 
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading) { return; } var testValue = g_form.getValue('test'); if (!testValue) { alert('Test cannot be empty!'); } }
Your feedback makes the community stronger! If you found this helpful, marking it as the correct answer helps others.
Stay awesome,
Roshnee Dash

Brad Bowman
Kilo Patron

If you are triggering this script onChange of a field other than the one named 'test', you can simply change your script to get the field value in the appropriate way:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   if (g_form.getValue('test') == '') {
      alert('test cannot be empty!');
   }
}

If this script is running onChange of the 'test' field, you need to remove the first appearance of newValue, so that the script runs, and you can use that syntax in place of g_form.getValue... if you want:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading) {
      return;
   }
   if (newValue == '') {
      alert('test cannot be empty!');
   }
}

 

Its_Azar
Kilo Sage

Hi there @SubbarayappS 


if (newValue == '') {
alert('Test cannot be empty!');

 

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.

Kind Regards,

Mohamed Azarudeen Z

Developer @ KPMG