Disable submit button via client script

Ariomar de Deus
Tera Contributor

I have an onchange client script and I need the submit button to be disabled if the field is empty. I tried this method, but it's not working. Could someone help me disable this field when it's empty?

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

    // Se o campo estiver vazio, desabilita o Submit
    if (!newValue || newValue.trim() === '') {
        g_form.setSubmitEnabled(false);
    } else {
        // Se houver imagem, habilita o Submit
        g_form.setSubmitEnabled(true);
    }
}
7 REPLIES 7

Brad Bowman
Kilo Patron

I think this approach is all wrong.  First off, you need to use actual g_form methods that exist, you can't make up new names

https://www.servicenow.com/docs/bundle/zurich-api-reference/page/app-store/dev_portal/API_reference/... 

Since you're referring to a 'submit' button, is this a new record on a form?  In that case the field in question, as all fields, will be empty to start so it may never change.

 

A simple out of box solution for what you're trying to accomplish is to make the field mandatory, then Submit or Update/Save will not be allowed if there is not a value.

Unfortunately, it's not possible to make this field mandatory because it needs to remain hidden. When I make it mandatory, it ends up becoming visible to the applicant.

@Ariomar de Deus 

if field is hidden on form then how it matters to user if it's empty

user can't fill it and it's annoying to stop them for something which they were not supposed or allowed to edit.

It's not good user experience.

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

This field is hidden because it receives the sys ID of a record, and I use this record in other scripts to load other information.