Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Allow String type variable to accept only integers

Rocky5
Kilo Sage

Hello Devs,

 

We have a string type variable in catalog item. And we want that variable to accept only integers greater than 8. I am using below regex in my script and it does not accept any number greater than 9. But I want that variable to accept any number greater than 8 but no less than 8. 

 

regex: /^[8-9]*$/

 

How can I achieve that?

 

Thanks,

Rocky.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Rocky5 

this regex worked for me

^(?:[9]|\d\d\d*)$

AnkurBawiskar_0-1716389235354.png

 

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

6 REPLIES 6

swathisarang98
Giga Sage

Hi @Rocky5 ,

 

You can try below onchange client script,

 

first it checks the regex and whether the input is a number or not then it check the input is greater than 8 ,

 

Change the backend name according to your catalog item

 

 

 

 

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

    var number = g_form.getValue('inte'); // Get the value of the integer field
    var regex = /^[0-9]+$/;// Define the regex to check if the input is a number

    // Check if the input matches the regex
    if (!regex.test(number)) {
        alert('Please enter a valid number.');
        return false;
    }

    // Convert the input to a number and check if it is greater than 8
    if (parseInt(number, 10) <= 8  )  {
        alert('The number must be greater than 8.');
        g_form.clearValue('inte');
        return false;
    }

    
    return true;


}

 

 

 

 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Community Alums
Not applicable

@Rocky5 ,

Please try regex=/^(?:[9-9]\d*|[1-9]\d{1,})$/

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar