Check for a single character validation upon selecting the choice in another variable.

satya30
Tera Contributor

Hi All,

Please help me and thanks in advance.

The requirement is on change of yes or no variable, the code that is entered on another variable needs to be validated. 
ie. If yes is selected for choice the  code variable needs to have a k in the entered code. This is required

Below is the script that I tried.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue === 'true' || newValue === true) {
       
        g_form.addInfoMessage("newValue: " + newValue);
       
        var Code = g_form.getValue('code');
       
               g_form.addInfoMessage("Code: " + Code);
       
       
        if (Code.includes('k')) {
            g_form.addInfoMessage('code contains "k": ' + Code);
            g_form.hideFieldMsg('code');
        } else {
           
            g_form.addInfoMessage('code does NOT contain k ');
            var msg = 'code must contain an "k"';
            g_form.setValue('code', '');  
            g_form.showFieldMsg('code', msg, 'error');
        }
    } else {
         g_form.hideFieldMsg('code');
    }
}
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@satya30 

try this

It's yes no variable so it won't give true/false value

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'yes') {
        var Code = g_form.getValue('code');
        if (Code.indexOf('k') > -1) {
            g_form.hideFieldMsg('code');
        } else {
            var msg = 'code must contain an "k"';
            g_form.showFieldMsg('code', msg, 'error');
            g_form.clearValue('code');
        }
    } else {
        g_form.hideFieldMsg('code');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@satya30 

try this

It's yes no variable so it won't give true/false value

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    if (newValue == 'yes') {
        var Code = g_form.getValue('code');
        if (Code.indexOf('k') > -1) {
            g_form.hideFieldMsg('code');
        } else {
            var msg = 'code must contain an "k"';
            g_form.showFieldMsg('code', msg, 'error');
            g_form.clearValue('code');
        }
    } else {
        g_form.hideFieldMsg('code');
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Viraj Hudlikar
Giga Sage

@satya30 

As you saying its yes no variable why you are checking true false value. First try to see what value is coming in newvalue when options are selected and on basis of that proceed with your logic. 

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

Thanks & Regards
Viraj Hudlikar.