How to restrict an integer field to not enter decimal values.

Pavan S
Tera Contributor

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.

 

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

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()
  }
   
}

 

 

 

View solution in original post

7 REPLIES 7

Kieran Anson
Kilo Patron

OOB the UI will display a warning to a user if they try to submit a form whereby the value in an integer field is invalid. Are you wanting to show a similar error message earlier (e.g onChange?). Are you finding the OOB solution delivered by SN insufficient, or is this a training issue?

KieranAnson_0-1714033549673.png

 

Hi Kieran,

 

yes, we have tried the below on change client script but it didn't worked.

 

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

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

        return;

    }

    var regex = /^[0-9]*$/;

 

    if (!regex.test(newValue)) {

        g_form.setValue('u_full_name', ''); //full name is the backend name of Number

    }

}

Hi @Pavan S,

 

This was actually the script I provided. I've tried and tested it on my PDI. I can only assume something is misconfigured, let me provide some screenshots 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

 

Screenshot 2024-04-25 at 10.32.15.png

Robbie
Kilo Patron
Kilo Patron

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()
  }
   
}