www.softwareriviera.com
Software Code
    New Code     Most Interesting     Don't Miss     Our picks  
JAVASCRIPT - section

View Article
Limit Textarea characters
Limit textarea number of characters using the following Javascript code
To limit the number of characters in a textarea control do the following:
- add the following Javascript code in your page
- add a text area whose name is 'cComment' as shown bellow

Javascript code:
<script language="javascript">
function checkNumberOfCharacters(theForm) {
var errorStr = "";
var maxlength = 255;
if (theForm.cComment.value.length > maxlength)
{
errorStr = "Warning!\nThe comment is too long.\nIt must be less than " + maxlength + " characters.";
}

if (errorStr) {
alert(errorStr);
return false;
}else
{
alert('The number of characters in the textarea is OK');
return false;
}
}
</script>

Html Code:
<form onsubmit="return checkNumberOfCharacters(this);">
<!-- add more controls if you like -->
<textarea name="cComment" cols="20" rows="3"></textarea>
<input type="submit" value="Check Number Of Characters">
</form>
Date: 2008-1-26    Written by: Jon Doe
     See all articles in this section
Copyright © 2007-2009 SoftwareRiviera.com. All rights reserved.  | Terms of use | Disclaimer | Privacy Policy |