We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Restricting character limit to 500 using client script

developer18
Tera Contributor

Hi All,

 

I have one Text field in Problem form .

I want to restrict user to enter only 500 characters if its more then it should not allow.

In Max length i have given 500 still its taken more than 500 and its known error in Servicenow

SO i have written on change client script as below

---------------------------------------------------------

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

var control = g_form.getControl('u_observation');
if (control.value.length > 500) {
//g_form.hideErrorBox('u_observation');
//g_form.showErrorBox('u_observation', 'You have reached the maximum limit of ' + 500 + ' characters for this field.');
alert('You can not put more than 500 characters to Observation field');
control.value = control.value.substring(0, 500);
} else {
g_form.hideErrorBox('u_observation');
}
}

-------------------------------------

As per onchange once i click outside of field then only alert is popping and characters after 500 getting deleted.

Is there any way i can restrict to enter only?

 

Regards,

Roopa 

1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

This solution still seems to work. 

https://servicenowguru.com/scripting/client-scripts-scripting/maximum-length-large-string-fields-ser...

Something like this in an onload script: 

function onLoad() {
    var control = g_form.getControl('u_observation');
    //Set its onkeyup method
    control.onkeyup = isMaxLength;

}


function isMaxLength() {
    var mLength = 100;
    var control = g_form.getControl('u_observation');
    if (control.value.length > mLength) {
        g_form.hideErrorBox('u_observation');
        g_form.showErrorBox('u_observation', 'You have reached the maximum character limit for this field.');
        control.value = control.value.substring(0, mLength);
    } else {
        g_form.hideErrorBox('u_observation');
    }
}

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

7 REPLIES 7

Michael Jones -
Giga Sage

This solution still seems to work. 

https://servicenowguru.com/scripting/client-scripts-scripting/maximum-length-large-string-fields-ser...

Something like this in an onload script: 

function onLoad() {
    var control = g_form.getControl('u_observation');
    //Set its onkeyup method
    control.onkeyup = isMaxLength;

}


function isMaxLength() {
    var mLength = 100;
    var control = g_form.getControl('u_observation');
    if (control.value.length > mLength) {
        g_form.hideErrorBox('u_observation');
        g_form.showErrorBox('u_observation', 'You have reached the maximum character limit for this field.');
        control.value = control.value.substring(0, mLength);
    } else {
        g_form.hideErrorBox('u_observation');
    }
}

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Thank u so much it works

the script which i have shown that did not work ?

 

Note: if you will use getControl() on portal, in future deployment, that will not work.