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

Hi @Thomas G,

 

You may need to use regex in here.
EX: var reg = /^\d+$/;
reg.test('1234567')

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

Hi Rohila,

I tried that. It gives me a parsing error (Unexpected token) in line 9. The line with g_form.setValue('u_account_number', ''); 

 

Best regards
Thomas

Hi 

Try this.

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.clearValue('u_account_number');
            alert('Please enter 8 or more characters.');
        }
    } else {
          g_form.clearValue('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

Hi again, 

 

By adding the number 8 after < in the line: 

if (myFieldValue.toString().length <

 This one seems to work. Thank you! 🙂

ThomasGammelga_0-1669711192800.png

Best regards
Thomas