Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Onchange client script is not working

chandan86patra
Tera Contributor

Hi All , 

 

I have written the on change client script  on the account field , on the first time this  script is working fine but in the second time changing the  field value ( giving more than 9 digits ) it is not clearing the value , could you please help me on this script :

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}

var regexp = /^\d{1,9}$/;

if (!regexp.test(newValue)) {
g_form.showFieldMsg(
'account_number',
'Account number must contain a maximum of 9 digits.',
'error'
);

g_form.setValue('account_number', '');
}
}

3 REPLIES 3

Its_Azar
Mega Sage

Hi there @chandan86patra 

 

The could be due to the onChange script triggering again when g_form.setValue() clears the field. 

You can simplify the validation like this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || !newValue)
        return;

    if (!/^\d{1,9}$/.test(newValue)) {
        g_form.showFieldMsg(
            'account_number',
            'Account number must contain a maximum of 9 digits.',
            'error'
        );
        g_form.setValue('account_number', '');
    }
}

It should work if it doesnt try using an onSubmit Client Script to validate the value before saving, or a UI Policy/.

☑️ If this helped, please mark it as Helpful or Accept Solution so others can find the answer too.

Kind Regards,
Azar
Serivenow Rising Star ⭐
Architect@ KPMG.

Hemanth M
Tera Sage

Hello @chandan86patra ,

 

can  you try these....

1 . clear the message upfront

2. use ClearValue instead setValue

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue == '') {
        return;
    }

    // Clear any existing messages 
    g_form.hideFieldMsg('account_number', true);

    var regexp = /^\d{1,9}$/;
    
    if (!regexp.test(newValue)) {
        g_form.showFieldMsg(
            'account_number',
            'Account number must contain a maximum of 9 digits.',
            'error'
        );
        // Use clearValue instead of setValue to cleanly reset the field
        g_form.clearValue('account_number');
    }
}

 

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025,2026

Ankur Bawiskar
Tera Patron

@chandan86patra 

try this

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

    var accountNumber = (newValue || '').trim();

    // Allow user to clear the field manually
    if (accountNumber == '') {
        g_form.hideFieldMsg('account_number', true);
        return;
    }

    var regexp = /^\d{1,9}$/;

    if (!regexp.test(accountNumber)) {
        // Clear first, then display the message
        g_form.clearValue('account_number');

        setTimeout(function () {
            g_form.showFieldMsg(
                'account_number',
                'Account number must contain a maximum of 9 digits.',
                'error'
            );
        }, 100);
    } else {
        g_form.hideFieldMsg('account_number', true);
    }
}

💡 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  ||  ✨ 10x ServiceNow MVP  ||  ✨ ServiceNow Community Leader