- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 12:10 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 12:24 PM
This solution still seems to work.
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!
Michael D. Jones
Proud member of the GlideFast Consulting Team!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 12:13 PM
use g_form.getValue() instead g_form.getControl()
Eg:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var control = g_form.getValue('u_observation');
if (control.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');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 12:18 PM
Thanks for reply Harsh,
i just tried it.. even same after clicking outside alert showing and its not deleting rest of characters also
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 12:20 PM
are you getting alert() correctly ?
to set the value, you have to use g_form.setValue('field name',result 😞
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 12:29 PM