PDA

View Full Version : Max length for textareas??


DiaH
09-19-2002, 09:03 PM
I have a need to use several textarea boxes. I need to limit them to the field size in sql. How can I do that? Or can I use a regular text box for multiple lines/scrollable?

Thanks,
Dia

boxer_1
09-19-2002, 09:12 PM
Here's a script you may want to have a look at for limiting the input of a textarea:

http://www.dynamicdrive.com/dynamicindex16/limitinput.htm

Any help?

Zvona
09-19-2002, 09:28 PM
http://www24.brinkster.com/zvona/textareaLength.html

DiaH
09-20-2002, 02:15 PM
Boxer_1...

How would I go about ripping that script apart to just use the restrict length function? I don't need/want the display part.

Dia

whammy
09-20-2002, 03:47 PM
I also have a textarea field limiter - unfortunately though if you want to limit the field in ALL browsers you'll need some kind of alert or something.

Better yet - if you're using SQL, then it's likely you're using a server-side language, right?

Why not limit the value of the textarea input on the server-side, and if they go over, just post the form again with an error saying "Yo, buddy! Don't type more that [whatever] characters, ok?"

:)

beetle
09-20-2002, 04:10 PM
I agree with Whammy mostly. If you are interacting with a database you need server-side data validation. What server-side language are you using? I have some PHP that can help you if you're using PHP

Also, if you have the datatype set properly in SQL, it will truncate any incoming data, i.e: if you send 'hello world' to a varchar(7) column, SQL will only store 'hello w'

DiaH
09-20-2002, 06:39 PM
I am using SQL Server with vbs on the server side. There's no issue using IE 6 or NS6.2. Both of those just truncate the data with no problem, no error. I tried the same thing on IE5.5 and I get a multi-step sql error which only occurs when the form is sending more data than acceptable for that field.

The script that boxer_1 pointed out works quite well actually. I'd rather stop them clientside b/c the users aren't too bright. If they just can't type anymore then they stop typing. That much they understand.

beetle
09-20-2002, 07:19 PM
Ah, I see. Ya, that script on DD is a good one. If you need any other client-side validation, be sure to check out my client-side validation project, fValidate (link in my sig)

whammy
09-21-2002, 04:57 PM
If I remember correctly though, the script you are referring to won't work in Opera or NS 4.x - if that's not a concern, great! If it is, I'd be sure to test them in the browsers you're worried about.

I'm fairly sure the textarea script I have is completely cross-browser - however in order to make it that way, I had to just trim the text AFTER they typed too much for some browsers. :(


<script type="text/javascript">
<!--
/*********************
Maxtext v1.2
© 2002 Robert K. Davis
**********************/
function textlen(x,y){
var thelength = x.value.length;
window.status=thelength+' of '+y+' maximum characters.';
}

function maxtext(x,y){
tempstr = x.value
if(tempstr.length>y){
x.value = tempstr.substring(0,y);
}
textlen(x,y);
}
/*********************/
// -->
</script>

<textarea
name="mytextarea"
cols="45"
rows="3"
wrap="virtual"
onkeypress="return(this.value.length<20)"
onkeydown="textlen(this,20)"
onkeyup="textlen(this,20)"
onblur="maxtext(this,20)"
>