Sert string field to minimum characters and numbers only

Thomas G
Tera Guru

Hi,

I have an onChange Client Script ensuring that minimum eight characters are entered in a string field.

 

It looks like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var myFieldValue = g_form.getValue('u_account_number');
if (myFieldValue.toString().length < 8 ) {
g_form.setValue('u_account_number','');
alert('Please enter 8 or more characters.');}
}

 

Now I want to expand that so that only numbers from 0 to 9 should be possible. How can I achieve that?

Best regards
Thomas

1 ACCEPTED SOLUTION

Hi Thomas

 

Did you try the regex option suggested by Jerick, that works.

or you can also do this too which is same as Validation regex but through script.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var regexp = /^[+]?\d*$/;
    var myFieldValue = g_form.getValue('u_account_number');
    if (regexp.test(newValue)) {
        if (myFieldValue.toString().length < 😎 {
            g_form.setValue('u_account_number', '');
            alert('Please enter 8 or more characters.');
        }
    } else {
        g_form.setValue('u_account_number', '');
        alert('Please enter Numbers');
    }
}

 

 


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

View solution in original post

16 REPLIES 16

Well, now I need to do the same for cell edits. I thought, that I could just make a copy of the onChange Client Script and set the copy to type OnCellEdit instead but when saving I get a message saying: 'Missing function declaration for onCellEdit'

 

What do I need to change in order to make the Client Script work on cell edits too?

 

ThomasG_0-1677742411946.png

 

Best regards
Thomas

I changed line 1 to: onCellEdit(sysIDs, table, oldValues, newValue, callback) {

Now I can save it as an OnCellEdit script but alas it does not work as expected. Now I can not set an Account Number on an incident from a list view at all. I can enter anything but it is not saved.

 

How can I make sure that the same rules applies on list edits for that field as for field changes on the incident form where I get an error message if less than eight numericals are entered?

 

Best regards

Murthy Ch
Giga Sage

Hi @Thomas G 

Can you please try below script:

 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var myFieldValue = g_form.getValue('u_account_number');
var pattern = new RegExp(/^[0-9]*$/);
if (!pattern.test(myFieldValue) || myFieldValue.toString().length < 😎 {
alert(getMessage("Please enter atleast 8 or more numericals only"));
g_form.clearValue("u_account_number");
}
}

 

 

 

Thanks,
Murthy

Sebastian L
Mega Sage

Hi Thomas,

If you have an on-change client script you can do it with a regex: 

 

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
  var checkNumberAndLength = /[0-9]{8,}$/g;   //The regex has "[0-9]" which says you can only enter numbers between 0 and 9. The "{8,}" is how many characthers you look for. Here we require minimum 8. In the below we test if the newValue (your variable) lives up to these requirements! 
	
  if(!checkNumberAndLength.test(newValue)) {  
    g_form.setValue('your_variable', ''); //Change your variable here - we set the field to blank
    g_form.addErrorMessage('Need new stuff'); //do some kind of error, or do it with a field message instead
  }  
   
}

 

 


Best regards,
Sebastian Laursen

I updated the script to look for 8 or minimum by adding a "," in the {8}


Best regards,
Sebastian Laursen