Limit for integer field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2017 02:29 AM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2017 05:10 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2017 06:00 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2017 06:05 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2017 06:21 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2017 06:27 AM
Hi Sam,
Can you check by adding value for result variable in alert statement?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader