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

Harish Murikina
Tera Guru

Hi Angus,



                    I have written the script to validate 6 digit number


                    1)The script allow only 6 digits, it wont allow 5 digits and space


                    2)But my script allows combination of numbers and characters you can do validation for this.




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


  if (isLoading || newValue == '')


  {


  return;


  }


  else


  {



  var regEx =   /(?!^0*$)(?!^0*\.0*$)^\d{1,7}?$/;


  var numb = newValue;


  if(!regEx.test(numb))



  {


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


  if(sreg.test(numb))



  {


  alert('Please dont enter space between or after digits. Enter exactly 6 digits');


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


  return false;


  }


  }



  else


  {


  if(numb.length == 6)


  {


  }


  else


  {


  alert('Please enter exactly 6 digits number ');


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


  return false;


  }


  }


  }




}






Regards,


Harish.


Thanks Harish, I'll have a play.


Brad Tilton
ServiceNow Employee
ServiceNow Employee

You should be able to remove the comma by adding format=none to the attributes on the dictionary entry for that record. I believe for the validation you can just use an onchange client script and use fieldvalue.length to validate the length.


Thanks Brad, that fixed that one.