Results 1 to 5 of 5

Thread: Restricting length of <textarea> in vbscript [RESOLVED]

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    2

    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:
    1. <script language = "vbscript">
    2.  
    3. dim mintReturnKeyCount
    4.  
    5. sub txaInput_onKeyPress
    6.     dim lintLimit 'character limit
    7.     lintLimit = 10
    8.    
    9.     if window.event.keyCode = 13 then 'catch return key
    10.         mintReturnKeyCount = mintReturnKeyCount + 2
    11.     end if
    12.    
    13.     if len(txaInput.value) - mintReturnKeyCount >= lintLimit then
    14.         msgbox "Maximum length is " & lintLimit & " characters."
    15.         window.event.returnValue = false
    16.     end if
    17. end sub
    18.  
    19. </script>
    Last edited by maximwinter; Oct 3rd, 2002 at 11:31 AM.

  2. #2
    Frenzied Member monte96's Avatar
    Join Date
    Sep 2000
    Location
    Somewhere in AZ
    Posts
    1,379
    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..

  3. #3
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    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">

  4. #4
    Hyperactive Member Anglo Saxon's Avatar
    Join Date
    Mar 2002
    Location
    Durham, UK
    Posts
    259
    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    2
    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
  •  



Click Here to Expand Forum to Full Width