How to make a field numeric only?

becks_kirk
Tera Contributor

In our incident form I need to set a field to only allow a numeric value in the format: xxxx.xx ...how?

1 ACCEPTED SOLUTION

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Becky,



You can create an OnChange client script on the field with script as. Please adjust field column name accordingly. In the ex below replace short_description with the exact column name.


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


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


          return;


    }


  var myVal = g_form.getValue('short_description');


  var matchPattern = /^\d{4}[.]\d{2}$/;


  var myResult = matchPattern.test(myVal);



  if(!myResult){


  g_form.clearValue('short_description');


  g_form.showFieldMsg('short_description','Required format is xxxx.xx','error');


  }




    //Type appropriate comment here, and begin script below


   


}


View solution in original post

2 REPLIES 2

Chuck Tomasi
Tera Patron

Make sure the field is of type Decimal or Floating point.



find_real_file.png


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Becky,



You can create an OnChange client script on the field with script as. Please adjust field column name accordingly. In the ex below replace short_description with the exact column name.


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


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


          return;


    }


  var myVal = g_form.getValue('short_description');


  var matchPattern = /^\d{4}[.]\d{2}$/;


  var myResult = matchPattern.test(myVal);



  if(!myResult){


  g_form.clearValue('short_description');


  g_form.showFieldMsg('short_description','Required format is xxxx.xx','error');


  }




    //Type appropriate comment here, and begin script below


   


}