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

Community Alums
Not applicable

Hi @Vee Jay Recana ,

This is what you will have to use :

https://community.servicenow.com/community?id=community_question&sys_id=430e9fa51b83641038739979b04b...

Mark my answer correct & Helpful, if Applicable.

Thanks,
Sandeep

Community Alums
Not applicable

Hello @Sandeep Dutta ,

I tried the solutions on the link you provided but it doesn't allow me to input 1000 and above. It only allows me to input up to 999. Also, it's not a variable. It's a field from the table.

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

Community Alums
Not applicable

Thank you @Aman Kumar ! It is now letting me to input up to 4 digit (9999). I just need to work on resitricting it to input up to 2000 only.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var regexp = /^\b([0-9]|[0-9][0-9][0-9]|1000)\b/; // Reg exp to validate numbers upto 4 places /^[0-9]{1,2}$/;

    if (!regexp.test(newValue) && regexp != '2000' ) //STRY0178407 > change from 100 to 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

    }

}