Integer Field Formatting

amacqueen
Mega Guru

I modifying a module we created in Service Now and I've added two integer fields one of which I want to restrict to 2 characters and the other to four, additionally I want the four character field to display a 1234 and not 1,234.

 

Can anyone suggest an easy way to do this please as I can't track it down in the WIKI?

1 ACCEPTED SOLUTION

Hi Angus,



You don't need the number validation if you're using an integer field as the dictionary entry already validates that, so we only need to worry about the length. Here's an onchange script that returns an alert and clears out the value of the field if it's not 4 digits.



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


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


      return;


  }


  if (newValue.toString().length != 4) {


      alert("Please enter a 4 digit number");


      g_form.setValue('fieldname', '');


  }


}


View solution in original post

10 REPLIES 10

Hi Angus,



                        This code can works . try this.




      var val = '123';


      var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;


      var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)/g;


      if (re.test(val))


      {


              if(numb.length == 4)


                {


                }


                  else


                        {


                            alert('Please entnter exactly 4 digits');


                            return false;


                        }


      }




      else


    {


        alert('Please enter only numbers')


    }



Regards,


Harish.


Harish I made the change you suggested however I get an error as follows:-



Javascript parse error as line (31) column (21) problem = invalid return



My script is:-



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


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


          return;


    }




    //Type appropriate comment here, and begin script below


   


}


var val = '123';




      var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;




      var re1 = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)/g;




      if (re.test(val))




      {




              if(numb.length == 4)




                {




                }




                  else




                        {




                            alert('Please entnter exactly 4 digits');




                            return false;




                        }




      }










      else




    {




        alert('Please enter only numbers')




    }



The error appears to be on the line that includes return false;



I've tried to debug it to no avail, any suggestions?


Hi Angus,



You don't need the number validation if you're using an integer field as the dictionary entry already validates that, so we only need to worry about the length. Here's an onchange script that returns an alert and clears out the value of the field if it's not 4 digits.



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


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


      return;


  }


  if (newValue.toString().length != 4) {


      alert("Please enter a 4 digit number");


      g_form.setValue('fieldname', '');


  }


}


Perfect Brad.



Many thanks


try no



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


{


  if (isLoading || newValue == '')


  {


  return;


  }


  else


  {




  var val = newValue;




  var re = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;



  if (re.test(val))


  {




  if(val.length == 4)


  {



  }


  else


  {


  alert('Please entnter exactly 4 digits');


  return false;


  }


  }


  else


  {


  alert('Please enter only numbers');


  }


  }


}