onSubmit client script to make boolean field mandatory

davilu
Mega Sage

I have a true/false field on a form that I want to make mandatory.  After reading through some posts, I came across this one, which recommended writing the following onSubmit client script:

function onSubmit() {
var value = g_form.getValue('searched_resources');
if(!value){
alert('Please check the checkbox before submitting a new HR Request');
return false;
}
}

I have UI Type set to All, but I can still submit this form without checking the checkbox.  What am i missing?  Thanks!

1 ACCEPTED SOLUTION

vinothkumar
Tera Guru

Can you try something as below and it will work

 

function onSubmit() {
//Type appropriate comment here, and begin script below SpaceIsThePlace
if (g_form.getValue('searched_resources')=='false') {

alert('Please check the checkbox before submitting a new HR Request');

return false;
}
}

View solution in original post

7 REPLIES 7

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi,

 

Any reason for not just using a UI Policy?

//Göran

hey Goran, I tried a UI Policy, but since it's true or false, it appears to always be filled in.  I know I could make a choice list field instead, but I like the look of the checkbox and am hoping that I can use an onSubmit client script to alert a user that the checkbox must be checked before submission.

You need to check !value && value=='false' in the if condition

Please mark this response as correct or helpful if it assisted you with your question.

vinothkumar
Tera Guru

Can you try something as below and it will work

 

function onSubmit() {
//Type appropriate comment here, and begin script below SpaceIsThePlace
if (g_form.getValue('searched_resources')=='false') {

alert('Please check the checkbox before submitting a new HR Request');

return false;
}
}