How To Set Maxlength For Textarea In Javascript ?


This tutorial we will learn how to set maxlength for textarea in javascript. That too in very easy ways.

Here, I am going to make very simple example to do this, you don't require import jquery. You have to just write the following javascript code. So I used onkeyup and onkeydown event of code javascript.

very easy to use using onkeydown and onkeyup event and make it character count validation in HTML. 

So let's just see bellow example and copy, paste.

How To Set Maxlength For Textarea In Javascript ? - Step By Step
Example:
<!DOCTYPE html>
<html>
<head>
	<title>How to set maxlength for textarea in javascript?- Php Coding Stuff</title>
</head>
<body>

<div class="container">

	<form>
		<textarea  name="message" placeholder="Write Here..." onkeydown="limitTextOnKeyUpDown(this.form.message,this.form.countdown,180);" onkeyup='limitTextOnKeyUpDown(this.form.message,this.form.countdown,180);'></textarea>
        You have
        <input readonly type="text" name="countdown" size="5" value="180"> chars are left
	</form>

</div>

<script type="text/javascript">
    function limitTextOnKeyUpDown(limitField, limitCount, limitNum) {
      if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
      } else {
        limitCount.value = limitNum - limitField.value.length;
      }
    }
</script>


</body>
</html>

I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!

close