how do i limit the amount of character a user can enter in a text box?

liunx

Guest
Hi everybody. I am making a form for work that is a survey asking the customers satisfaction and I have these two questions.<br />
<br />
1 - how do i limit the amount of characters a user can enter in a text box?<br />
<br />
2 - how do i limit the amount of characters a user can enter in a text area box?<br />
<br />
im assuming both are done the same way...<!--content-->maxlength="2"<!--content-->excellent. thanks!<!--content-->no prob<!--content-->it didnt work for a text area box though :(<!--content-->huh I thought it did. I have to go but Ill post more tomorrow if someone else does not find a solution. I know there is a way to do it. I just cant think of it off the top of my head.<!--content-->ok thanks anyways. does anyone know a good website where i can find all the info on forms and form manipulation?<!--content-->Unfortunately, <textarea> elements do not support the maxlength attribute which all <input> elements do support. However, you can always use JavaScript to emulate this effect. For example, you could listen for an onkeydown event and if the total amount of characters exceeds the maximum amount you want you could simply replace the value with the first 10 characters for example, or whatever the maximum limit of characters is. Here's how it would be done:<form action="#"><br />
<textarea onkeydown="this.value=this.value.substring(0, 100);" rows="10" cols="50"></textarea><br />
</form>This assumes the maximum characters that are allowed in the textarea is 100, simply change that number to suit your needs. Also note, while this is a generally good client-side solution, there are ways around it, so I would strongly suggest doing a server-side check as well. <br />
<br />
I hope that helps you out.<!--content-->that worked like a charm. thanks pal!<!--content-->Hi!<br />
<br />
Fredmv's solution works properly except when the limit is reached and the user wants to make changes somewhere in the text.<br />
<br />
The following code is taken from: <!-- m --><a class="postlink" href="http://javascript.internet.com/forms/limit-textarea.html">http://javascript.internet.com/forms/li ... tarea.html</a><!-- m --><br />
That means, it is not mine!!!<br />
There are a few modifications I made. Here is the code:<br />
---------------------<br />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><br />
<html><br />
<head><br />
<title>Untitled</title><br />
<script type="text/javascript"><br />
function textCounter(field, countfield, maxlimit) {<br />
if (field.value.length > maxlimit)<br />
field.value = field.value.substring(0, maxlimit);<br />
else <br />
countfield.value = maxlimit - field.value.length;<br />
}<br />
</script><br />
</head><br />
<body><br />
<center><br />
<form name=myform action="#" onSubmit="textCounter(document.myform.message,document.myform.remLen,125);"><br />
<font size="1" face="arial, helvetica, sans-serif"> ( You may enter up to 125 characters. )<br><br />
<textarea name=message wrap=physical cols=28 rows=4 onKeyDown="textCounter(this.form.message,this.form.remLen,125);" onKeyUp="textCounter(this.form.message,this.form.remLen,125);" onMouseDown="textCounter(this.form.message,this.form.remLen,125);" onMouseUp="textCounter(this.form.message,this.form.remLen,125);"></textarea><br />
<br><input type=submit value="Submit"><br />
<input readonly type=text name=remLen size=3 maxlength=3 value=125> characters left</font><br />
</form><br />
</center><br />
</body><br />
</html><br />
---------------------<br />
I left out the copyright stuff and so to make the code easier to read - not to leave the impression, that it is based on my idea.<br />
<br />
The onmouseup and -down makes the function work as well, when some user inserts text by right clicking and pasting additional stuff which would only be eliminated, when the user does some onkeydown afterwards. The onsubmit="" will do a final check of the input length on form submission.<br />
<br />
I particularly liked the counter :)<br />
<br />
Cheers - Pit<!--content-->wow. thats very nifty stuff. got it to work with my form like a charm . thanks !!<!--content-->Hi!<br />
<br />
To Marlon: Glad, that you like it. Don't forget - it's not yours and not mine; it was somebody else who did the original thing. But afaik everybody may use it.<br />
<br />
To fredmv: Just wanted to mention, that I was surprised, seeing your version making nonsense when the limit is reached and the user...<br />
<br />
Would have done it the same way like you and I don't see a reason for that behaviour. I just do not want you to think, that I considered your suggestion some sort of ****ty! ;)<br />
<br />
Cheers - Pit<!--content-->I understand, I didn't come accross any problems, however.<!--content-->Thanks, fredmv!<br />
<br />
Not everything I contribute in this forum is really substantial - I'm not a pro! And I'm having a language problem too.<br />
<br />
Being misunderstood would be a pity.<br />
<br />
Cheers - Pit<!--content-->Basically I did not know that the maxlength attribute was not supported for a text box but since I do all of my forms in asp.net I have always just restricted the amount of space in a feild and I have found myself using the mid statement before to cut off excess text. Now, this would be a more secure option for you if you were to script something like that server side because the user will be able to go around that max length that you are doing in java script if they do not support java script, thus allowing them to enter pages apon pages of data into that tiny little text area. If you have access to a server side language someone here can get you the syntax for that and help you in other ways with it, I might be able to depending on what your server supports.<!--content-->well im using asp to take all the data in the form and transfer it to an access database. all the users that fill out the form should have javascript in their browser but if they dont then they will just get an error message after they hit the submit button anyways im assuming if they enter in anything more than 255 characters for the text area boxes. reason why its 255 is because thats as big as access will allow for one piece of data in text format. i have never used asp.net, im doing asp classic with vbscript<!--content-->you can have more then 255 in an access data base. Infact you can have unlimited. Change your text feilds in access to memo. It will give you unlimited text and numbers etc. The draw back to using this is users can fill up your data base if you do not cap it. I, like I said, use a sort of mid statement. But this statement is just added into the parameters on my incert sql statement so its not stand alone or anything. I think you should do something like that though. You can make a java script alert or use a label or something if the user enters too much text but then fall back on a statement to add extra security. Just my thoughts suggestions / opinions.<!--content-->i dont have any idea how to do that stuff.<br />
<br />
i will change those from text to memo though<!--content-->Basically its just a simple string statement. I am not really the greatest resource on vbscript but in vb regular it would be<br />
x = Mid(string, startposition, numchars)<br />
for example<br />
<br />
label1.text = Mid("Hello", 2, 3)<br />
output is<br />
ell<br />
<br />
label2.text = Mid("Hello",1,4)<br />
output is<br />
Hell (tehehe)<br />
<br />
Basically this second one is what you want. Start it on the first character and ignore everything after 4 characters. I suspect it will be the same in vbscript so try it. Also that "Hello" can be replaced with a text1.text or a label etc.<!--content-->
 
Back
Top