views:

601

answers:

3

Hi,

Is it possible to have a multi-line textbox (im using asp.net c#) where the maximum characters inputted cannot exceed the visible textbox size?

I have taken scrollbars off vertically (overflow: hidden).

Now I want it so that say if the multi-line textbox shows say 100px height (or say 5 rows), the user cannot type more than the height of the textbox?

There must be a JS/JQuery hack for this?

+1  A: 

No, it is not possible.

Because ASP.NET textbox finally turns into HTML textarea which does not support text limit.

You can do this trick with JavaScript, sure, but what concerns visible area, it may not be so easy. You will need to somehow calculate metrics of the current font in use, then try to render in memory to see if the limits of the box are exceeded. One could such tricks when programming for Windows, but with web pages it is likely not possible.

Developer Art
+1 seems like there is no solution.
waqasahmed
A: 

Although it finally turns into a textarea you COULD do this, but it is hacky.

With JQuery on textarea keypress, check length of value, if it is over your threshold, remove last character.

Like I said hacky.

Good luck,

Dan

Daniel Elliott
virtually impossible that way because some characters have different with than others (e.g. m is bigger than l), therefore cannot accurately based the visible typable textbox as the max length based on the number of chars. Thanks though.
waqasahmed
A: 

You can limit text max. char as following.

Client Side

function ValidateLength()
{
    if(document.forms[0].txtFlightRemarks.value.length > MAX_LENGTH)    
    {           
        document.forms[0].txtFlightRemarks.value = document.forms[0].txtFlightRemarks.value.substring(0,MAX_LENGTH);
        document.forms[0].txtFlightRemarks.focus();
        return false;
    }    
}

Server Side (Page_Load): attach onchange and onkeyup events to required textarea.

txtFlightRemarks.Attributes.Add("onchange", "ValidateLength();");
txtFlightRemarks.Attributes.Add("onKeyUp", "ValidateLength();");
Ahmed
nice try, but this is not what I am looking for.
waqasahmed