Show alert onSubmit if field is not empty

paul971
Mega Expert

How can I set an onSubmit client script alert to display if a field is not empty? I'm not finding anything relevant other than things for catalog items which gives an error.

8 REPLIES 8

justin_drysdale
Mega Guru

function onSubmit() {


    //Type appropriate comment here, and begin script below


    var your_field = g_form.getValue('u_field_name');



    if (your_field == '') {


          alert('u_field_name is blank');


    }


}


bernyalvarado
Mega Sage

Hi Paul,



You can navigate to Client Scripts, and create an onSubmit client script in there.



Thanks,


Berny


bernyalvarado
Mega Sage

The following should be helpful:


http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices


http://wiki.servicenow.com/index.php?title=Client_Scripts#gsc.tab=0



Quote from the wiki:


3.2 onSubmit() Scripts  

An onSubmit() script runs when a form is submitted. Typically, you use an onSubmit() script to validate things on the form and ensure that the submission makes sense. As such, onSubmit() scripts can potentially cancel a submission by returning false.


An onSubmit() script must contain a function named onSubmit().


For example, here is an onSubmit() script that prompts the user to confirm that a priority one ticket should really be submitted. If the user clicks Cancel in the confirmation dialog box, the submission is canceled.



function onSubmit() { var priority = g_form.getValue('priority'); if (priority && priority == 1) return confirm('Are you sure you want to submit a priority one ticket? The CIO will be notified!');} }


The important thing to remember here is: to stop form submission, return false from your onSubmit() script.



Thanks,


Berny


prithvirajchaud
Mega Expert

HI,



Right click on the header -> configure -> client scripts -> new


From there choose onSubmit in the type field.


The code would be somewhat like this



function onSubmit(){


        if(g_form.getValue("<field_name>")){


                  alert("Your text here");


        }


}



Thanks