How to terminate the string characters in service now

String
Kilo Sage

Hi Team,

we trying to restricting the use for 20 characters for note field ,So we written client script onChange .we need to stop typing after 20 character and show an alert .but user  enter more than 20 characters also ,Script not showing any alert 

 

Below Is my code :

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

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

        return;

   var dec = g_form.getValue('notes');

// Calculate the byte length of the string using TextEncoder

    var byteLength = new TextEncoder().encode(dec).length;

 if (byteLength > 20) {

       alert("notes length exceeds 4000 bytes');

        return false;

    }}

Please guide me 

5 REPLIES 5

Anand Kumar P
Giga Patron
Giga Patron

Hi @String,

Try below script 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    var dec = g_form.getValue('notes');
    var charLength = dec.length;

    if (charLength > 20) {
        alert("Note length exceeds 20 characters");
        g_form.setValue('notes', dec.substring(0, 20));
    }
}

Mark it helpful and solution proposed if it serves your purpose.

Thankse,

Anand 

Hi @Anand Kumar P  thanks for your quick reply ,Is there  any way to show alert immediately after exceeding 20 character while user entering the characters in notes field .

Hi @String ,

 

Try with g_form.showFieldMsg('<field name>', '<your custom message>', 'info');

Hi @String ,

You can use 

g_form.addInfoMessage("myObj");
or
g_form.showFieldMsg('<variable_name>', 'msg', 'info', false);

Mark it as solution proposed and helpful if it serves your purpose.

Thanks,

Anand