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

try this

var pattern = /^([5][555]{0,2}|1000)$/; it will allow 5,555,1000

Regards
Harish

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Ruchi,

why you want to use regex if you can simply compare it

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

// ok

}

else{

// invalid

}

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

Hi Ankur,

Thanks for the response.

Initially i tried this but as part of best practices Regex was expected in review.

Regards,

Ruchi

Hi Ruchi,

yes it is good practice to use regex for complex calculation but for simple you can use the comparison itself

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

Hi Ankur,

I noticed this is working fine but it's not checking for value if it is digit or not. Because of which the field is accepting alphabets as well.

-Ruchi