Limit for integer field

Sam Ogden
Tera Guru

Hi all,

I've used the below on change client script to inform users to enter values between 1 and 25.

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

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

          return;

        g_form.hideFieldMsg('u_risk_based_testing_score');

        var newVal = parseInt(newValue);
        if (newVal < 1 || newVal > 25) {
                  g_form.showFieldMsg('u_risk_based_testing_score', 'Values must be between 1 and 25', 'error');

        }

}

If I enter values 1 - 25 these are fine.   The error message then appears for values 26 - 999.

Then values 1,000 - 25,999 these do not show the error, then numbers 26,000 and above do.

Do you know what would cause this?

Alternatively is there a way to stop users adding more than 2 digits into the integer field?

What would I need to do to stop a user being able to save the form if they enter a value that is not between 1 and 25?

Thanks

Sam

18 REPLIES 18

Hi Sam,



The onChange client script will only work when user moves the control from the field and not while typing etc.



Regards


Ankur


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

Hi Sam,



Any update on this?


Can you mark answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.



Regards


Ankur


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

Hi Ankur ,

I have similar requirement where user can put the values from 1 to 100 only I have single line text field on Catalog Item,

could you please help with the script , I am not that much familiar with scripting part.

 

truzicki
Kilo Expert

Hi Sam.   Not sure if you still have this question, but here's a pretty simple way to accomplish this:



First, go into the dictionary entry for the field in question, and add format=none to the attributes (you may need to click the "advanced" form link to see to the attributes field).   This will remove the comma from the field.



Second, create an onChange client script that looks like this:



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


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


                  return;


        }



        //Determine if the field is greater than two digits and prevent that from occurring


        if (newValue.length > 2) {


                  alert('You may not enter a number larger than two digits');


                  g_form.setValue('u_your_field', 0);


                  return;


        }



        //Determine if the field is greater than three digits and prevent that from occurring


        if (isNaN(newValue)) {


                  alert('You may only use numeric characters in this field');


                  g_form.setValue('u_your_field', 0);


                  return;


        }



        //Determine if the value is greater than 25 and then prevent that from occurring


        if (newValue > 25) {


                  g_form.setValue('u_your_field', 0);


                  alert('You cannot enter more than 24 hours in a day');


                  return;


        }



}



Hope this helps!