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

Amit Verma
Kilo Patron
Kilo Patron

Hi @sariksorte 

 

Refer below post and change the regex as 

^(0?[1-9]|1[0-7]|)\b$

https://www.servicenow.com/community/developer-forum/validating-single-line-text-field-to-allow-ente...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Viraj Hudlikar
Giga Sage

Hello @sariksorte 

 

I am not sure what your bug as I cannot see anything in screenshot as bug.

You can use below regex for your requirement:

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

 

Check below thread which will give idea how to use it:
Service Portal Catalog Items: Regex Field Validati... - ServiceNow Community

Regex Validation - ServiceNow Community

 

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.

Hello @sariksorte 

 

Follow below steps:
1) Create record under Variable Validation regex table.

VirajHudlikar_0-1742444676148.png

 

2) Use same regex in your variable created as show below:

VirajHudlikar_1-1742444760634.png

 

3) Now showtime check over portal:

VirajHudlikar_2-1742444832389.png

 

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.

It is working for 2 to 16 but it is not working for 1.

sariksorte_0-1742449090083.png