Workflow and ServiceDesk Community

 View Only

Keeping Count of Text Length in a TextBox 

Oct 01, 2015 01:19 PM

Original article is posted here.

jdeleon7010 posted a topic about restricting text length in a text element on a Workflow form.

Does workflow have a builtin component for specifying a character limit in a multiline text box?

I did this once, a long time ago – so I pulled out my workflow archives, swept the dust off, and cringed at some of my old work.  I found the javascript I needed for the demo, though.

The attached gif demonstrates the countdown (and count up, when removing characters) method to inform a user just how much space remains in the box.

I wasn’t able to find the old code before jdeleon7010 sorted out a workable javascript solution, but I thought it prudent that I archive the method a bit better and post it here.

Here’s what you’ll need for this demo:

  • A TextBox with a Control Id
  • A Label with a Control Id (in the attached demo package it’s an AsciiMergeLabel, but a Label will work the same way)
  • Set the “Text” value for the Label at the initial (empty) textbox count (in this example, it’s set at “500 Characters Remaining”)

Here’s the javascript that goes in the Script section of your form:

function textLenCount(tarElem, tarLabel) {
var inputField = document.getElementById(tarElem);
var inputLabel = document.getElementById(tarLabel);
if (inputField != null) {
if (inputField.value.length > 500) {
inputField.value = inputField.value.substring(0, 500);
inputLabel.innerHTML = '0 Characters Remaining';
}
else {
inputLabel.innerHTML = 500 - inputField.value.length + ' Characters Remaining';
}
}
}

And here’s the javascript to leverage the “oninput” function of the TextBox.  Be sure to give both the TextBox and the Label Control Ids.  This code goes in the Body Custom Events section of your form.

var inputField = document.getElementById('InputField');
inputField.setAttribute('oninput','textLenCount("InputField", "ReadField")');

This code will also prevent more characters than the prescribed limit (in this case, 500 characters (see attached gif)).

Statistics
0 Favorited
0 Views
2 Files
0 Shares
2 Downloads
Attachment(s)
gif file
textcount1.gif   16 KB   1 version
Uploaded - Feb 25, 2020
gif file
textcount2.gif   16 KB   1 version
Uploaded - Feb 25, 2020

Tags and Keywords

Related Entries and Links

No Related Resource entered.