Jim Coyne
Kilo Patron

Here's a UI Script you can use in the Service Catalog to set the maximum length of a string variable to limit how many characters a user can enter:



Name: u_setMaxLength
Description: Function to set the "maxlength" attribute on an input field
Active: Checked
Global: Checked
Script:
function u_setMaxLength(variableName, size) {
try{
var fieldName = g_form.getControl(variableName).name.toString();
if (Prototype.Browser.IE) {
fieldName.placeholder = size;
} else {
$(fieldName).writeAttribute('maxlength', size);
}
} catch(err) {}
}


You can then call it from an onLoad client script:


function onLoad() {
u_setMaxLength("test123", 5);
}


And now users are limited to just 5 characters for that variable.

It can actually be used in normal forms as well, but probably more useful with Catalog Variables as you cannot define their lengths. Attached is the UI Script XML file for import into your instance.

6 Comments