TextBox MaxLength Validation and Display Remaining Characters

Text Box OR Text area max length validation and also display the words remaining.
Arguments:
(1) TextBox or Text Area ID
(2) Label ID where we want to show count of remaining character.
(3) Number of Maximum character that are allowed.

Sample Code:
function CountCharactersGeneral(sourceTextBox, displayControl, maxLength)
{
if(sourceTextBox != null && displayControl != null)
{
sourceTextBox = document.getElementById(sourceTextBox);
displayControl = document.getElementById(displayControl);
if(sourceTextBox != null)
{
var len = sourceTextBox.value.length
if (len<=maxLength)
{
displayControl.innerHTML = maxLength -len +" Character(s) remaining.";
}
else
{
sourceTextBox.value = sourceTextBox.value.substring(0, maxLength);
return false;
}
}
}
}

0 comments: