We can limit the maximum characters in <input> with <maxlength> attribute. However
it is not possible to do the same thing with <textarea>. This will be a problem if value of the <textarea> will be stored into database table and the length of the user input exceeds the field's length, either the value will
be truncated or your database will throw an error to you.
Fortunately, a little jquery script can help here:
Fortunately, a little jquery script can help here:
<html> <head> </head> <body> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready ( function() { $("textarea[maxlength]").keypress ( function() { var maxlength = $(this).attr("maxlength"); if ( this.value.length > maxlength ) { this.value = this.value.substring(0,maxlength-1); } } ) } ) </script> <form> This text box is limited to 5 chars <textarea maxlength="5"></textarea><br> This text box is not limited <textarea></textarea> </form> </body> </html>
Comments
Post a Comment