Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to limit number of characters in a textarea field processing php?

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?

like image 620
user1373168 Avatar asked May 06 '12 06:05

user1373168


2 Answers

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.

like image 52
JoBaxter Avatar answered Oct 13 '22 23:10

JoBaxter


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.

like image 44
s.webbandit Avatar answered Oct 13 '22 23:10

s.webbandit