Add this function in JavaScript on your page, I stuck it my "functions.js" file I include on the page anyway.
function checkMaxLen(txt,maxLen) {
try{
if(txt.value.length > (maxLen-1)) {
var cont = txt.value;
txt.value = cont.substring(0,(maxLen -1));
return false;
};
}catch(e){
}
}
Then on the textbox use something like this:
<asp:TextBox runat="server" ID="txtComments" CssClass="comment_textbox" Height="75px" TextMode="MultiLine" onkeyup="return checkMaxLen(this,151)"></asp:TextBox>
Notice the function passes the textbox (this) and the maxlength you want, in this case 150, notice I have 151, the function will limit the maxlength minus one... So if you want to limit the textbox with mutiline set to 100, you enter 101, make sense?
5 comments:
Hello zubair,
Its a nice idea though but user can have javascript disabled on his browser, so using regex can be a much better option, try using ^[\s\S]{0,900}$ for not accepting more than 900 characters.
Regards,
Its working. Thank u so much.
Its working. Thank u so much.
You have to ask why Microsoft after 4 versions of .NET hasn't built this in to ASP.NET
There's actually a 1 line code behind solution to this bug in the framework. You can simply set the 'maxlength' attribute in the back end as an attribute, which then gets rendered in the HTML and the browser recognises.
MaxLength code behind solution
Post a Comment