Regex that accepts 4 digits from 0 to 9

Community Alums
Not applicable

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'); 

   }

}

1 ACCEPTED SOLUTION

Aman Kumar S
Kilo Patron

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 🙂

Best Regards
Aman Kumar

View solution in original post

9 REPLIES 9

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 🙂

 

Best Regards
Aman Kumar

Community Alums
Not applicable

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 🙂

Community Alums
Not applicable

Hello @Aman Kumar ,

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');

    }

}

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}))$/

Best Regards
Aman Kumar

Community Alums
Not applicable

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');

    }

}