Regex to enter value in a range

Rj27
Mega Guru

Hi All,

Can any one please assist me to write a regex to validate if value entered is in between 5 to 1000.

Tried few things but it's not working in script.

Many thanks!

-Ruchi

1 ACCEPTED SOLUTION

Manish Vinayak1
Tera Guru

Hi Ruchi,

If you want to use RegEx to validate those limits you can try:

var pattern = /^([5-9]|[1-9][0-9]|[1-9][0-9][0-9]|1000)$/g;

alert(pattern.test("999"));

Tried it for few combinations, seems to be working fine. Try that out and see if it works for you.

Hope this helps!

Cheers,

Manish

 

View solution in original post

11 REPLIES 11

Hi Ruchi,

so you can use this first to check whether it is an integer or not and then compare the range

if(Number.isInteger(value)){

if(value >5 && value < 1000){

// ok

}

else{

// invalid

}

}

else{

alert('Please put only integer values');

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Manish Vinayak1
Tera Guru

Hi Ruchi,

If you want to use RegEx to validate those limits you can try:

var pattern = /^([5-9]|[1-9][0-9]|[1-9][0-9][0-9]|1000)$/g;

alert(pattern.test("999"));

Tried it for few combinations, seems to be working fine. Try that out and see if it works for you.

Hope this helps!

Cheers,

Manish