tejesht Posted September 2, 2006 Share Posted September 2, 2006 Hello everybody, is it possible to set maxlength for textarea tag so that user can only write comments up to the max length. please suggestThanks in AdvanceTejesh Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 2, 2006 Share Posted September 2, 2006 You'll want to use javascript for that. Heres an example:[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Untitled Document</title>< script type="text/javascript" >function postlimit(){ var maxlength = 500 if (document.form1.txtarea.value.length > maxlength) { alert("You have typed the maximun amount of characters for your post.\n\n" + maxlength + " Character max per post, this includes the use of BBCode and Smilies"); document.form1.txtarea.value = document.form1.txtarea.value.substring(0, maxlength); } else { document.form1.counter.value = maxlength - document.form1.txtarea.value.length; }}< /script ></head><body><form name="form1" action="" method="post"> <textarea name="txtarea" onkeydown="postlimit()" cols="45" rows="8"></textarea><br /> <small>Characters left: <input type="text" name="counter" size="4" disabled="disabled" value="500" /> Max 500 characters</small></form></body></html>[/code]Code key:maxlength - holds the max number fo characters allowed to be entered in the textarea.form1 - the name of the form (add name="formNameHere" in the form tag) that contains the textareatxtarea - the name of the textareacounter - the name of the form field called counter which shows how many characters are left Quote Link to comment Share on other sites More sharing options...
oldmanice Posted September 8, 2006 Share Posted September 8, 2006 If your talking about apache then Namewidth=* Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted September 8, 2006 Share Posted September 8, 2006 If you implement the javascript option then you rely on the client having js on.But that is the only real option to limit on teh client side. You shoudl also check the length of teh string submitted in your processing script - if its longer than you allow then direct them back to the page and tell them that the text is too long. Quote Link to comment Share on other sites More sharing options...
.josh Posted September 10, 2006 Share Posted September 10, 2006 if you were going to choose one or the other, then choose toonmariner's method. use php to check the length of the string submitted, and redirect to the form with an error saying it's too long. That way, you will not be at the mercy of the user having js enabled. But ideally, you should implement both options. Do the js option because it saves you bandwidth and makes it more convenient for the user, but also use php to check it as well, just in case the user has js disabled. Quote Link to comment Share on other sites More sharing options...
webFani Posted September 13, 2006 Share Posted September 13, 2006 [b]wildteen88 [/b] script rocks man :D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.