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,



This script should allow only 1 or 2 digit number only and uses regular expression and will work in client script.


var newVal = parseInt(newValue);


var validate = new RegExp('^[0-9]{1,2}$');


var result = validate.test(newVal);



result will be true if it is 1 digit or 2 digit number and false if more than 2 digits.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,



Thanks for the above.   I currently have the following onChange script:



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


        }


}




can your script above be added into this or do I need a separate script?



Thanks


Hi Sam,



It can be incorporated in the same script.


Modified script below:



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


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



return;



g_form.hideFieldMsg('u_risk_based_testing_score');



var newVal = parseInt(newValue);



var validate = new RegExp('^[0-9]{1,2}$');


var result = validate.test(newVal);



if (!result) {


g_form.showFieldMsg('u_risk_based_testing_score', 'Values must be between 1 and 25', 'error');


}


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,



How is the above meant to work.   If I add values 1-25 no messages come up as expected.


26 - 99 I get the warning ' 'Values must be between 1 and 25'




However it still allows me to put 3 digits in:


find_real_file.png



or 4 digits:



find_real_file.png


Hi Sam,



Can you check by adding value for result variable in alert statement?



Regards


Ankur


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