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

If the record should not be submitted if this field is blank and the field is updated by a different script/process you can either not allow this field to be updated to blank by that process, so prevent the update upstream, or return 'false' in an onSubmit Client Script.

Since the field is hidden, what is changing the field to trigger your onChange Client Script?  This use case is not making a lot of sense.

Ankur Bawiskar
Tera Patron

@Ariomar de Deus 

that's not recommended approach.

you can use onSubmit script and stop form submission based on field value

💡 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