variable length restriction and validation

SnowDEV2
Kilo Expert

Hi All,

 

I have a requirement where one variable should accept only numbers and should be limited to 10 characters.

I have wrote a below onchange script to accept only numbers and onload script for field length.

Unfortunately onload script is not working.. any help please........

 

OnChange

 

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

 

      var callBack = g_form.getValue('callback');

 

     

              if(callBack.match(/\D/)){

                     

                      alert('Please enter only numbers!');

                      g_form.setValue('callback,'');

              }

       

     

}

Onload

 

function onLoad() {

  var call_back = g_form.getControl('callback');

      alert(call_back);

  call_back.maxLength = 10;

}

6 REPLIES 6

geoffcox
Giga Guru

What table is this running on? We have no fields in our database named "callback". Therefore I suspect this is a custom field, and that it's real name is probably "u_callback". Unless this is a service catalog variable?



If this is a database field, then you can set the maximum length by personalizing the dictionary.


Slava Savitsky
Giga Sage

To limit input length, add max_length=10 to the variable attributes.


Variable Types - ServiceNow Enterprise Wiki


justin_drysdale
Mega Guru

onChange vs onLoad:


Your onChange is running simultaneously as an onLoad AND as an onChange.


You need to put an 'isLoading' check in to prevent it running on page load:



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


  if(!isLoading) {


      var callBack = g_form.getValue('callback');


              if(callBack.match(/\D/)){


                      alert('Please enter only numbers!');


                      g_form.setValue('callback,'');


              }


  }


}



Regular Expression:


Your regular expression "/\D/" will only match NON digit characters.   To match a digit use /\d/, to match only 10 digits, use /\d{10}/.   I'd personally also start matching at the start of the string, resulting in: /^\d{10}/.



You are using .match() when a .test() seems to fit better for this use case.   I recommend using .test() as this returns either true or false.   .match() returns an array of results if a match is successful.   Either should work for this case but .test() is tremendously faster (more so for larger record sets).



Try:


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


  if(!isLoading) {


      var callBack = g_form.getValue('callback');


              if(callBack.test(/^\d{10}/)){


                      alert('Please enter only numbers!');


                      g_form.setValue('callback,'');


              }


  }


}




Form Variable:


If you have to restrict the variable length to 10 characters/digits, set it from the variable's variable attributes (max_length=10) and not from an onLoad script.   If it were me I'd completely remove the onLoad.


Hi


Its giving me an error while using   above field



JavaScript parse error at line (6) column (40) problem = unterminated string literal


Please hit the thumb Icon and mark as correct in case I help you with your query!!!
- Kailas