I am using a a textarea field in a form that is made on html and the processing is done by php. I want to limit the number of characters the user can enter in the textarea. Is there any way by which this can be done because the textarea field is then stored into a mysql database and thus i have to limit the user data according to the varchar value of the field.
Any help?
This is a very easy way to limit the length of a text area via JavaScript.
text area field:
<input class='textField' type='text' NAME='note1' value='' onkeyup='charLimit(this, 40);'>
JavaScript function:
function charLimit(limitField, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);}
}
When the user types past 40 characters it just removes them.
You can limit this by setting maxlength
attribute on textarea tag like <textarea maxlength="120"></textarea>
in HTML and additionally "cut" input string in PHP by substr()
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With