Displaying Length Of A String Field

Displaying Length Of A String Field

I am displaying the length of a string field by creating a Notes field like this:

<div style="text-align: right"><span style="color: rgb(153, 153, 153)"><font size="2">0/30</font></span></div>

and using an On User Input script like this which truncates the field to a particular length or displays the updated length in the Notes field:

if(input.Headline_1.length() > 30)
{
input.Headline_1 = input.Headline_1.mid(0,30);
}
else
{
input.Headline_1_Char_Count = "<div style=\"text-align: right;\"><span style=\"color: rgb(153, 153, 153);\"><font size=\"2\">" + input.Headline_1.length() + "/30</font></span></div>";
}

Is there a better way of doing this? Can a live length be displayed as each character is entered?