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

Harish KM
Kilo Patron
Kilo Patron

try this

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

var pattern = /^([5-9][5-9]{0,2}|1000)$/; // number between 5 to 1000
if(!pattern.test(newValue)){
g_form.showFieldMsg('number', 'Please enter a number between 5 to 1000', 'error');
}
}

Regards
Harish

Hi Harish,

This is working fine. 

Thanks!

-Ruchi

Glad it worked for you. Also mark the appropriate answer as correct 

Regards
Harish

Hi Harish,

I tried with few values and noticed it is not working as expected. I can only enter values that doesn't contain numbers 0-4, eg 555 would be accepted but not 554 or 500.

Regards,

Ruchi