I have the following bug in the code please if anyone have solution for it please suggest

sariksorte
Tera Contributor

I have a requirement that the value in #CPU should be more than 1 and less than 17 

I achieved that using regex but it is still showing below bug. How to fix this?

1 ACCEPTED SOLUTION

@sariksorte 

something like this should work

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

    g_form.hideFieldMsg('variableName');
    newValue = parseInt(newValue);
    var regex = /^(?:[2-9]|1[0-6])$/;
    if (!regex.test(newValue)) {
        g_form.showFieldMsg('variableName', 'Please enter a number between 2 and 16.', 'error');
        g_form.clearValue('variableName');
    }
}

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

10 REPLIES 10

@sariksorte 

something like this should work

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

    g_form.hideFieldMsg('variableName');
    newValue = parseInt(newValue);
    var regex = /^(?:[2-9]|1[0-6])$/;
    if (!regex.test(newValue)) {
        g_form.showFieldMsg('variableName', 'Please enter a number between 2 and 16.', 'error');
        g_form.clearValue('variableName');
    }
}

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

Hello @sariksorte 

 

Just update regex as below:

^(1[0-6]|[1-9])$

 

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.

sariksorte
Tera Contributor

Hi Viraj,

I have attached one image in attachment 

 

J Siva
Tera Sage

Hi @sariksorte 
Hope you get a solution from other community members.
We can also achieve this by using below on-change client script.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
var val = parseInt(newValue);
if(val<1 || val>17){
	g_form.clearValue("cpu_count");
	g_form.showFieldMsg("cpu_count","Invalid input","error");
}
}

JSiva_0-1742445240310.png


Hope this helps.
Regards,
Siva

Ankur Bawiskar
Tera Patron
Tera Patron

@sariksorte 

you didn't share any script which you are using then how can we help?

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