Field character limiter
6
This snippet also you to limit the input in a form field to a specified number of characters. It displays a counter so users can see how many characters they have left, and once they reach the limit the field just trims the length to your limit.
The following is a snippet from what I used when I implemented a tagboard to my site. Further revisions could/should read the LIMIT from the maxlength attribute
The following is a snippet from what I used when I implemented a tagboard to my site. Further revisions could/should read the LIMIT from the maxlength attribute
<form id="tagboard" action="url/here/" method="post">
<fieldset>
<legend>Tagboard</legend>
<label for='name'>Name:</label>
<input type="text" id="name" name='name' />
<label for='message'>Message: </label>
<span id='charleft'></span>
<textarea id='message' name='message' onkeyup='go()' onkeydown='go()' rows='' cols=''></textarea>
<input id='tag-submit' type="submit" name="submit" value="Tag IT!" />
</fieldset>
</form>
function go()
{
// Number of characters
var LIMIT = 50;
// Get the elements to fiddle with
var textarea = document.getElementById('message');
var span = document.getElementById('charleft');
// if the textarea has less letters then the
// limit, update span with the number of characters
if (textarea.value.length <= LIMIT)
{
var newspan = parseInt(LIMIT) - parseInt(textarea.value.length);
span.innerHTML = newspan;
}
// if the textarea has more, trim down to LIMIT
else
{
textarea.value = textarea.value.substring(0, LIMIT);
}
}






1. Select really large block of text
2. Click into your field
3. Press right menu button to activate context menu and select "Paste" command.
The whole text block will be placed into the field.
"Since data fields usually have a maximum size when they are input into a SQL database, this code is useful to ensure that users' input conforms to the limitations of the data storage engine."
I want to warn everybody that this is utter bullshit. If I just would disable javascript, your database will vomit over my input. Always, repeat after me, ALWAYS check user input SERVER SIDE before doing anything furter with it. NEVER trust the user his input. Paranoia keeps your webapp alive.