New Zealand's new Copyright Law presumes 'Guilt Upon Accusation' and will Cut Off Internet Connections without a trial. CreativeFreedom.org.nz is against this unjust law - help us


A Space for All Things .NET Related


How to validate length of input into a multi-line textbox

By James Hippolite, in , posted: 26-Aug-2008 08:18

You can validate the length of input of an ordinary TextBox using the MaxLength attribute.  However, this doesn't work if the TextBox has a TextMode of Multi-line.  In those instances, use this piece of code.

<script type="text/javascript" language="javascript">

   

    function MaxLength()

    {

        var txt = $get("<%=ResponseNote_TextBox.clientid %>");

        var msg = $get("<%=Message_Label.clientid %>");

       

        if (txt.value.length > 500)

        {

            SelectText();

                                                           

            msg.className = "Failure";

            msg.innerHTML = "* Response has more than 500 characters";

        }

        else

        {

            if (msg.innerHTML == "* Response has more than 500 characters")

            {

                msg.innerHTML = "";

            }

        }

    }

   

    function SelectText()

    {

            var txt = $get("<%=ResponseNote_TextBox.clientid %>");

            var rng = txt.createTextRange();

 

            rng.moveStart("character", 501);

            rng.moveEnd("character", 0);

            rng.select();

    }

   

    function RemoveCRLFs()

    {

   // debugger;

    var txt = $get("<%=ResponseNote_TextBox.clientid %>");

    var str = window.clipboardData.getData("Text");

   

    str = str.replace(/(\s*(\r?\n|\r))+$/, '');

   

    txt.value = str ;

       

    }   

       

       

</script>        

 

Page_load:

ResponseNote_TextBox.Attributes.Add("onblur", "javascript:MaxLength()")

ResponseNote_TextBox.Attributes.Add("onpaste", "javascript:RemoveCRLFs()")

 

Other related posts:
Geek Post Monthly Newsletter Volume 2 Issue 6
Exam 70-300 Objective 3
COALESCE T-SQL Function






Add a comment

Please note: comments that are inappropriate or promotional in nature will be deleted. E-mail addresses are not displayed, but you must enter a valid e-mail address to confirm your comments.

Are you a registered Geekzone user? Login to have the fields below automatically filled in for you and to enable links in comments. If you have (or qualify to have) a Geekzone Blog then your comment will be automatically confirmed and shown in this blog post.

Your name:

Your e-mail:

Your webpage:

JamesHip's profile

James Hippolite
Wellington
New Zealand


Welcome to my technical blog. 

Here, I attempt to distill the Microsoft Certified Professional Developer knowledge I have accumulated since first qualifying MCP in 1996.  This blog started on 13 September 2007 as an off-shoot from my mixed up personal blog.  But it took a shot in the arm from Scott Hanselman's talk at TechEd New Zealand 08 "32 Ways To Make Your Blog Suck Less".