- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 12:30 AM
We are having an integer type field in a table, and we want to restrict that field to not enter the decimal values.
We have tried a client script for the string type field but unable to do for integer type field.
Can anyone help?
Please find the attachment of the field type info.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 01:37 AM
Hi @Pavan S,
I'm a little confused here. With a field type of 'Integer', ServiceNow will not allow decimals or string characters to be saved within this field. My only assumption here is that you would like to validate at the time of entering with the use of a Client Script - see below for an example.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regex = /^[0-9]+$/;
if(!regex.test(newValue)) {
g_form.clearValue('u_test_integer'); //adjust the field name appropriately
g_form.setValue('u_test_integer','');
g_form.showFieldMsg('u_test_integer', 'Only integer numbers allowed. Decimals not permitted', 'error');
g_form.showFieldMsg()
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 02:33 AM - edited 04-25-2024 02:49 AM
Hi @Pavan S ,
You can create onchange client script on the integer field as shown below, Tested in my Pdi its working
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var regExTest = /(^100$)|(^([1-9]([0-9])?|0)$)/g;
//change the backend name according to your config
if (!regExTest.test(newValue)) {
alert("The price allows only integer values");
g_form.clearValue('u_full_name');
g_form.showFieldMsg("u_full_name", "The price allows only integer values", "error");
}
}
Please mark this comment as Correct Answer/Helpful if it helped you.
Regards,
Swathi Sarang
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 02:36 AM
Hi @Pavan K2,
So my response isn't lost in the thread, see the below tried and tested Client Script from my PDI. I can only assume something is misconfigured on your implementation. See the below screenshot to help you.
To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.
Thanks, Robbie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2024 03:19 AM
Hi @Pavan S ,
Create a onChange client scirpt for field with below script :
function onChangeIntegerField(control, oldValue, newValue, isLoading) {
if (newValue !== '' && newValue.indexOf('.') !== -1) {
alert("Decimal values are not allowed in this field.");
control.setValue(oldValue); or clear the value...
}
}
☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....