BlueDevil23
09-18-2009, 04:42 AM
Here a few little functions I made to use with a form, to tell the user how many characters they have left. Obviously, they can bypass this without JS turned off, so make sure to check the length of the submitted string, server-side also!
/**
* xBrowser_getID returns the supplied elements ID
*
* This is basically a cross-browser version of get.ElementById()
*
* @param string supplied_ID This is the element you want to get the ID of.
* @return The ID of the supplied element.
* @type string
*
*/
function xBrowser_getID(supplied_ID)
{
var ID = null;
if(document.layers)
{
ID = document.layers[supplied_ID];
}
else if(document.all)
{
ID = document.all[supplied_ID];
}
else if(document.getElementById)
{
ID = document.getElementById(supplied_ID);
}
return ID;
}
/**
* fillCounter is meant to be used in conjunction with characterCounter, to prefill.
*
* fillCounter finds the element provided, and counts the number of characters
* in it. It then finds the element with the counterID and fills it with the
* correct number depending on the maximum allowed characters. This function is
* meant to be used after document execution and without any user interaction --
* to simply prefill the element, for better presentation.
*
* @requires xBrowser_getID() Requires this function to get the ID of the supplied element.
*
* @param string elementID Element that you are counting the characters in.
* @param string counterID Element that is updated with the characters left.
* @param integer max_allowed_chars The maximum amount of characters allowed in the elementID.
*
*/
function fillCounter(elementID, counterID, max_allowed_chars)
{
var element = xBrowser_getID(elementID);
var counterElement = xBrowser_getID(counterID);
counterElement.innerHTML = max_allowed_chars - element.value.length;
}
// Example: fillCounter('profileSig', 'lettersLeft', 300);
/**
* characterCounter sets a value to be counted up and down.
*
* characterCounter finds the element provided, and sets the max characters allowed
* in it. It is meant to be used with the onkeyup and onkeydown event handlers
* When the user either keys up or down, it will decrease the character count
* by the amount of character written. If the characters exceed the maximum
* allowed, it will subtract them from the input, and bring them back down to
* the allowed amount.
*
* @requires xBrowser_getID() Requires this function to get the ID of the supplied element.
*
* @param string elementID Element that you are counting the characters in.
* @param string counterID Element that is updated with the characters left.
* @param integer max_allowed_chars The maximum amount of characters allowed in the elementID.
*
*/
function characterCounter(elementID, counterID, max_allowed_chars)
{
var element = xBrowser_getID(elementID);
var counterElement = xBrowser_getID(counterID);
if(element.value.length > max_allowed_chars) {
element.value = element.value.substring(0, max_allowed_chars);
}
else {
counterElement.innerHTML = max_allowed_chars - element.value.length;
}
}
Usage:
<form action="#" method="POST" id="dummy_form">
<textarea row="5" cols="60" id="dummy_text" onkeyup="characterCounter('dummy_text', 'counter', 300);" onkeydown="characterCounter('dummy_text', 'counter', 300);"> Heya MakeWebGames </textarea>
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
<span id="counter"></span>
<script type="text/javascript">
fillCounter('dummy_text', 'counter', 300);
</script>
/**
* xBrowser_getID returns the supplied elements ID
*
* This is basically a cross-browser version of get.ElementById()
*
* @param string supplied_ID This is the element you want to get the ID of.
* @return The ID of the supplied element.
* @type string
*
*/
function xBrowser_getID(supplied_ID)
{
var ID = null;
if(document.layers)
{
ID = document.layers[supplied_ID];
}
else if(document.all)
{
ID = document.all[supplied_ID];
}
else if(document.getElementById)
{
ID = document.getElementById(supplied_ID);
}
return ID;
}
/**
* fillCounter is meant to be used in conjunction with characterCounter, to prefill.
*
* fillCounter finds the element provided, and counts the number of characters
* in it. It then finds the element with the counterID and fills it with the
* correct number depending on the maximum allowed characters. This function is
* meant to be used after document execution and without any user interaction --
* to simply prefill the element, for better presentation.
*
* @requires xBrowser_getID() Requires this function to get the ID of the supplied element.
*
* @param string elementID Element that you are counting the characters in.
* @param string counterID Element that is updated with the characters left.
* @param integer max_allowed_chars The maximum amount of characters allowed in the elementID.
*
*/
function fillCounter(elementID, counterID, max_allowed_chars)
{
var element = xBrowser_getID(elementID);
var counterElement = xBrowser_getID(counterID);
counterElement.innerHTML = max_allowed_chars - element.value.length;
}
// Example: fillCounter('profileSig', 'lettersLeft', 300);
/**
* characterCounter sets a value to be counted up and down.
*
* characterCounter finds the element provided, and sets the max characters allowed
* in it. It is meant to be used with the onkeyup and onkeydown event handlers
* When the user either keys up or down, it will decrease the character count
* by the amount of character written. If the characters exceed the maximum
* allowed, it will subtract them from the input, and bring them back down to
* the allowed amount.
*
* @requires xBrowser_getID() Requires this function to get the ID of the supplied element.
*
* @param string elementID Element that you are counting the characters in.
* @param string counterID Element that is updated with the characters left.
* @param integer max_allowed_chars The maximum amount of characters allowed in the elementID.
*
*/
function characterCounter(elementID, counterID, max_allowed_chars)
{
var element = xBrowser_getID(elementID);
var counterElement = xBrowser_getID(counterID);
if(element.value.length > max_allowed_chars) {
element.value = element.value.substring(0, max_allowed_chars);
}
else {
counterElement.innerHTML = max_allowed_chars - element.value.length;
}
}
Usage:
<form action="#" method="POST" id="dummy_form">
<textarea row="5" cols="60" id="dummy_text" onkeyup="characterCounter('dummy_text', 'counter', 300);" onkeydown="characterCounter('dummy_text', 'counter', 300);"> Heya MakeWebGames </textarea>
<input type="submit" id="submit" name="submit" value="Submit" />
</form>
<span id="counter"></span>
<script type="text/javascript">
fillCounter('dummy_text', 'counter', 300);
</script>