How to validate glide list field on the client side

jmweli
Kilo Expert

Hi ServiceNow Developers,

 

I am looking for a way if there is one, to check and make sure that there is a value on a glide list field before it is submitted. In other words I am looking for something like getValue() that will check the   value of a field and if the result of getValue() is spaces / empty then I issue an alert / error message and force the users to enter something. Is there a way I can validate the glide list field using client script to check and make sure that there is something on this field before the form is submitted. Please advise.

 

Thanks,

Johannes

1 ACCEPTED SOLUTION

Works as well, should have thought about this earlier


So the modified code would be:


function onSubmit() {
         if (g_form.isNewRecord()){
                   g_form.hideFieldMsg('u_list');
                   if(g_form.getValue('u_list')==''){
                             g_form.showFieldMsg('u_list','List is empty.','error');
                             return false;
                   }
         }
}


Kind regards,


Sascha


View solution in original post

6 REPLIES 6

B_39
Tera Guru

Hi Johannes,



Maybe I am not getting you correct, but wouldn't it be possible to create a UI policy for this case?


Just create a UI policy, that checks if the field is empty. If it is empty, set the field mandatory in the ui policy action.



Kind regards,


Sascha


jmweli
Kilo Expert

Hi Sascha,



Thanks for your response on my question. The UI policy won't work here because I need to make sure that before they submit the form there is at least some value on the glide list field. In other words I need something like an onsubmit client script however I can not code one up because it looks like ServiceNow has not client side APIs for glidelist fields. Right now I am doing this validation using a business rule but I was just wondering if there is a way I can do it client side.



Thanks,


Johannes


Hi Johannes,



Maybe you could check the options of the select element related to your glide list. Check them in an onSubmit client script and cancle the submit if the length is less 1 (e.g. at least one option is existing):



function onSubmit() {
         if (g_form.isNewRecord()){
                   var checkSelect = $('select_0incident.u_list'); //using the id of your glide lists select element
                   g_form.hideFieldMsg('u_list');
                   if(checkSelect.options.length$lt;1){
                             g_form.showFieldMsg('u_list','List is empty.','error');
                             return false;
                   }
       
         }
}


I have only tested it in Chrome. You would need to make browser specific tests for FF and IE.



Kind regards,


Sascha


marcguy
ServiceNow Employee
ServiceNow Employee

Hi,



The field stores multiple values in a comma seperated list of sys_ids. i.e. if you have 4 values in there it would store


sys_id1,sys_id2,sys_id3,sys_id4


So a g_form.getValue('fieldname');


would return that value so you could check in a client script for illegal characters and spaces and alert if it contains any of them?