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

swathisarang98
Giga Sage
Giga Sage

Hi @Pavan S ,

 

You can create onchange client script on the integer field as shown below, Tested in my Pdi its working

swathisarang98_0-1714037560165.png

 

 

 

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

Robbie
Kilo Patron
Kilo Patron

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

 

Robbie_0-1714037704370.png

 

 

Sohail Khilji
Kilo Patron
Kilo Patron

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....

LinkedIn - Lets Connect