Reputation: 3835
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?
Upvotes: 1
Views: 1689
Reputation: 17960
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();");
Upvotes: 0
Reputation: 22857
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
Upvotes: 0
Reputation:
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.
Upvotes: 1