|
-
Oct 2nd, 2002, 02:09 AM
#1
Thread Starter
New Member
Restricting length of <textarea> in vbscript [RESOLVED]
Hi pals, I'm new to this forum.
I'm trying to limit the maximum character count of a <textarea> box in a html web form. I'm collecting user comments from that box, and I don't want excessive amount of text.
I tried to check the length of the text with some client side vbscript. I checked the length of text whenever onKeyPress event is fired, But the problems are --
- The text length returned by len() includes the return key counts (carriage return key + blank line, 2 extra count for every line break), but I need to count the exact number of characters only.
- I tried to use a static variable to count how many times return key was pressed (If window.event.keyCode = 13 then ...). Afterwards, I subtract that variable from the total length to get the text length excluding the return key counts. But this will not work if the user decides to delete what he just typed in the textarea. The carriage return and blank lines will be deleted, but the return key count will remain the same.
- I can't find a way to catch the keycode when a user presses backspace key. Even if I can, the user can still do selection with shift key and delete the whole chunk of text in just one stroke of backspace key.
I've searched through some online resources and found client side codes to do that in javascript. But one of the requirements my school project is that I must use strictly vbscript only.
Any help will be appreciated. Just a link to a web page or a forum thread will do. Thanks in advance.
VB Code:
<script language = "vbscript">
dim mintReturnKeyCount
sub txaInput_onKeyPress
dim lintLimit 'character limit
lintLimit = 10
if window.event.keyCode = 13 then 'catch return key
mintReturnKeyCount = mintReturnKeyCount + 2
end if
if len(txaInput.value) - mintReturnKeyCount >= lintLimit then
msgbox "Maximum length is " & lintLimit & " characters."
window.event.returnValue = false
end if
end sub
</script>
Last edited by maximwinter; Oct 3rd, 2002 at 11:31 AM.
-
Oct 2nd, 2002, 10:49 AM
#2
Frenzied Member
No. You need the return keys as well. They are part of the text. As well as TAB characters. If you save it to a SQL database's text datatype, all of those characters are written and must be considered in the counts.
Backspace you have to trap using OnKeyDown not OnKeyPress. Same for say a function key..
oOOo--oOOo
__ /\/\onte96
oOOo--oOOo
Senior Programmer/Analyst
MCP
[email protected]
[email protected]
Your results may vary.. some restrictions may apply.. pricing and participation may vary.. not available in all states.. professional driver closed course..quantities limited..
-
Oct 2nd, 2002, 10:59 AM
#3
Frenzied Member
Here is an idea:
In order to post user comment to the database the form should be submitted, RIGTH???
Code:
<script language="vbscript">
Sub SubmitMyForm()
if len(txaInput.value)>255 then
msgbox "Please cut that message ..."
else
document.form1.Submit()
end if
End Sub
</script>
'here is your submit button code
<input type="button" value="submit" onclick="SubmitMyForm()">
'here is your form, you should give it a name "form1"
<form method="POST" action="yourfilename.asp" name="form1">
-
Oct 3rd, 2002, 09:45 AM
#4
Hyperactive Member
I wrote this awhile ago for someone, you are welcome to it :
Code:
<html>
<head>
<title>Textarea length test</title>
<script language=vbscript>
function VBmaxChars(Obj, maxCharCount)
dim message,curCharCount
message = replace(Obj.value,vbcrlf,"")
curCharCount = len(message)
if curCharCount >= 1 then
vbcounter.innerHTML="characters remaining : <br>" & (maxCharCount - (curCharCount))
if curCharCount > 20 then
vbcounter.style.color="#cc0000"
else
vbcounter.style.color="#999999"
end if
else
vbcounter.innerHTML="characters remaining : <br>" & maxCharCount
vbcounter.style.color="#999999"
end if
if curCharCount > maxCharCount then
msgbox "You cannot use more than " & maxCharCount & " characters!",16,"Maximum input exceeded!"
Obj.value=left(message,maxCharCount)
vbcounter.innerHTML="characters remaining : <br>0"
end if
end function
</script>
</head>
<body style="font-family:verdana">
<form>
<table>
<tr>
<td class="input" valign="top">
<div class="divborder" id="vbcounter" align="center" style="color : #999999">characters
remaining : <br>
30</div>
</td>
<td colspan="3">
<textarea style="font-family:verdana" name="vbtext" id="vbtext" cols="62" rows="5" onkeyup="VBScript:VBmaxChars vbtext, 30"></textarea>
</td>
</tr>
</table>
</form>
</body>
</html>
---
Anglo Saxon
-
Oct 3rd, 2002, 11:28 AM
#5
Thread Starter
New Member
I wrote this awhile ago for someone, you are welcome to it :
At line 38, I believe you actually typed "vbscript[column][space]VBmaxChars vbtext, 30"
and the nasty forum shows --
onKeyUp="vbscript :VBmaxChars vbtext, 30"
That little ":" gave me a type mismatch error. 
Thanks a lot. Your function is almost exactly what I was searching for.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|