- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2022 02:08 AM
Hello Everyone,
I'm having a hard time creating a regex validation to accept 4 integers from 0 to 2000. Below is my current onChange client script. Currently, it's not working. It doesn't accept anything. Can anyone help me with this? Thank you.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regexp = /^\d{4}$/;
if (!regexp.test(newValue) && newValue != '2000' )
{
g_form.clearValue('unit_quantity');
g_form.showFieldMsg('unit_quantity', 'This field should contain only numbers - with a limit to 2000', 'error');
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2022 03:09 AM
Hey,
Use below regular expression:
\b([0-9]|[0-9][0-9][0-9]|1000)\b
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2022 04:07 AM
Hey,
Slight change in the regular expression, I overlooked the 2000 limit
^((2000)|([0-1]?[0-9]{1,3}))$
Feel free to mark correct, If I answered your query.
Will be helpful for future visitors looking for similar questions 🙂
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-14-2022 07:39 AM
Now, it just allow me to input up to 999 only
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regexp = /^((2000)|([0-1]?[0-9]{1,3}))$/; // Reg exp to validate numbers upto 4 places /^[0-9]{1,2}$/;
if (!regexp.test(newValue)) //STRY0178407 > change from 100 to 2000 && regexp != '2000'
{
g_form.clearValue('unit_quantity');
g_form.showFieldMsg('unit_quantity', 'This field should contain only numbers - with a limit to 2000', 'error'); //change from 100 to 2000 > STRY0178407
}
}
Apologies for the trouble 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2022 12:16 AM
Hello
I was trying to explore the regular expression you've given me so that it will meet the conditions "newValue should not exceed 2000" and/or "regular expression should be met" but so far, it still allows me to input up to 999 only. Please remember that I'm trying to do this in a field of table that's why I can't use the regex validation for the variable.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regexp = /^((2000)|([0-1]?[0-9]{1,3}))$/;
var number = parseInt(newValue);
if (!regexp.test(newValue) || number > 2000)
{
g_form.clearValue('unit_quantity');
g_form.showFieldMsg('unit_quantity', 'This field should contain only numbers - with a limit to 2000', 'error');
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2022 01:06 AM
The Regex is absolutely fine, if you want to test it in real time, you can also try in https://regex101.com/ with regex /^((2000)|([0-1]?[0-9]{1,3}))$/
Aman Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-19-2022 07:23 PM
What works for me
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var num = newValue;
var limit = parseInt(2000);
num = num.replace(/,/g, '');
var regexp = /^[-,0-9]{1,3}$/; // Reg exp to validate numbers upto 4 places
if (!regexp.test(num) && num > limit) {
g_form.clearValue('unit_quantity');
g_form.showFieldMsg('unit_quantity', 'This field should contain only numbers - with a limit to 2000', 'error');
}
}