Wrox Programmer Forums
|
ASP Forms As of Oct 5, 2005, this forum is now locked. Please use "Classic ASP beginner" at http://p2p.wrox.com/forum.asp?FORUM_ID=54 or "Classic ASP Professional" http://p2p.wrox.com/forum.asp?FORUM_ID=56 instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP Forms section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 19th, 2004, 05:43 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to restrict the input in TEXTAREA

Hi,

We can restrict the input length by using "maxlength" keyword, as follows:
<input type="text" name="dependant_name" size="50" maxlength="50">

How can I restrict the input length in <TEXTAREA>
As for example if I use
<textarea rows="2" name="project_task" cols="79"></textarea>

suppose I have a column in a table, ProjectTask with size text(100). If I store the value (project_task given in the example above) I will get an error. So I need to restrict the length <= 100 to avoid the error.
Could you please help me?
Thanks in advance


 
Old February 19th, 2004, 08:09 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Unfortunately the textarea input control doesn't support that. You could use one of the keypress event handlers in the textarea control to check the length of the text value each time the user presses a key.

<script>
function checkInputLength(objTextArea){
    if(this.value.length>100){
        alert('value is too long.')
        //put more code here, maybe you want to
        //actually chop off the extra characters
    }
}
</script>

<textarea ... onKeyUp="checkInputLength(this);" >

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old February 19th, 2004, 05:45 PM
Registered User
 
Join Date: Feb 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Agreed. The text area will not limit amount you can enter. Especially not if you cut and paste. Be sure you have server side code that takes the leftmost XX characters, so as not to cause an insert error. For example; myText = Left(64, txtTextArea);

This trims your text at 64 characters, especially if your database is expecing a varchar(64)...

 
Old February 20th, 2004, 08:44 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Another thought on this:

Although the standard HTML textbox control doesn't restrict the length you can enter, you could use a client side .NET validator. I forget if there is one ready-made for length. You could easily bake one up with the regular expression validator to check for length on-the-fly and have it pop a message up next to the text area when you reach the limit.

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old February 21st, 2004, 06:38 AM
Registered User
 
Join Date: Feb 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter and us7892 for your valued suggestion.

Peter's suggestion to use a javascript function and a on-the-fly check of the length is wonderful.

us7892's suggestion to cut the length is useful too. It will protect the program to crash from "data too long" problem...speacially where javascript is not supported.

Take care
:)

 
Old March 5th, 2004, 02:11 AM
Authorized User
 
Join Date: Dec 2003
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to MAMBONO
Default

This other javascript code works pretty well.
  <SCRIPT language=JavaScript>
<!--
    n = (document.layers) ? 1 : 0;
    ie = (document.all) ? 1 : 0;
    ver4 = (n || ie) ? 1 : 0;
    var n6 = ((document.getElementById)&&(!ie)) ? 1 : 0;
    var maxLen = 255;
    var enable;

    function Enablecounter() {
            enable = setInterval("counter()",500);
        }

    function counter()
        {
        var msLen, clLen,niLen, totLen, tmp;
        var done = 0;
        msLen = document.details.comments.value.length;
        //clLen = document.details.callback.value.length;
        //niLen = document.details.nick.value.length;
        //status=document.details.comments.value.length;
        //totLen = msLen + clLen + niLen;
        totLen = msLen;

        tmp = maxLen - totLen;
        if (document.details.charcount.value != tmp) {
            if (tmp > 0) {
            // update counter
            document.details.charcount.value = tmp;
            } else {
                if (tmp == 0) {
                    // alert once and update counter to be 0
                    alert("Your message is too long");
                    document.details.charcount.value = 0;
                    clearTimeout(enable);
                    document.details.comments.value = document.details.comments.value.substring(0,maxLen );
                } else {
                    document.details.charcount.value = 0;
                    document.details.comments.value = document.details.comments.value.substring(0,maxLen )
                }
            }

        }

    }

    function check()
    {
        if (document.details.charcount.value == "0")
            {
            alert("Your message is too long");
            return false;
            }
        if (document.details.comments.value=="" || document.details.comments.value==" ")
            {
            alert("Please type in your message");
            return false;
            }

    }




// End -->

</SCRIPT>

The value in red is the maximum amount of characters that you want to be typed in

You will need to give yur text area these properties for the javascript to work.

<textarea onBlur=Enablecounter() onFocus=Enablecounter() name=comments rows=7 wrap=virtual cols=50 onChange=Enablecounter()>






Similar Threads
Thread Thread Starter Forum Replies Last Post
Tab function + <textarea></textarea> Adam H-W HTML Code Clinic 0 April 24th, 2007 10:36 AM
restrict to paste values only jcellens Excel VBA 1 July 18th, 2006 11:36 AM
Restrict # of input Char. to TextFiled/ComboBox dontknowmuch Java GUI 2 February 7th, 2006 12:27 AM
display vbCrLf's from textarea to textarea mat41 Classic ASP Basics 8 June 10th, 2004 12:19 AM
Restrict elements guozhang XML 0 April 28th, 2004 02:26 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.