How to set the limit in a integer field value

utkarsh6
Giga Contributor

Hi All,

I have a question how to put the limit on any integer field. Like in a field we only put the integer value but I also want to specify that field like user can only enter min 5 digit or max 7 digit numeric value. I have already written the On-change catalog client script for only entering the integer value but also want to limit the number of digits.

 

Request you to please help or guide.

 

Thanks/Utkarsh

7 REPLIES 7

Priyanka Vasant
Tera Guru

Try using On chnage client script as below.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

 

    if (isLoading || newValue === '')

 

          return;

       var newVal = parseInt(newValue, 10);

 

        if (newVal < 100 || newVal > 200) {

 

                  g_form.showFieldMsg('field_name', 'Values must be between 100 and 200', 'error');

        }

}

Mark helpful or correct based on impact

 

Regards,

Priyanka A.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Utkarsh,

if you are still looking for solution to this?

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Vinay Krishna1
Kilo Guru

Hi Utkarsh,

 

You can achieve it using RegExp. I've tested it, and it should work. 

 

var field=g_form.getValue('variablename');

var validate=new RegExp("^(\\d{5,7})$");

if(validate.test(field))

//returns true if it the "field" value is is a digit, whose length is anything between 5-7.

 

Mark ✅ Correct and click Helpful if this satisfies your requirement.