0

I have this textbox that has the limit in DB for input 255. And I am trying to built a function in JS that stops the user for inserting more than 255 characters in that textbox.

Here is the asp.net code:

<asp:TextBox Id="textbox" TextMode="MultiLine"
             oninput="F(id,this.value.length, 255)"
             runat="server" Columns="25" Rows="5"></asp:TextBox>

And here is the code in JS(just a test):

this.textCounter = function (ref,value, maxlimit) {
    if (value > maxlimit) {
        $('#textbox').value=" ";
    }
    return true;
}

Somehow the $('#textbox').value is undefined. What I can do?

1
  • If you are using jQuery then it has method val for getting/setting value i.e. $('#textbox').val("aaaa"); for setting and $('#textbox').val(); for getting. value is a property in javascript and your are using jQuery object thus your code was not working. Commented Jan 9, 2017 at 11:00

4 Answers 4

2

You can use maxlength HTML property in your asp.net control. and that will work fine.

maxlength="255"

Edit:

<asp:TextBox Id="textbox" onKeyPress="return(this.value.length < 255);" runat="server" TextMode="MultiLine" Columns="25" Rows="5"></asp:TextBox>
Sign up to request clarification or add additional context in comments.

4 Comments

@GeorgeGreat can you please paste the full code with MaxLength propety ?
This one? public virtual int MaxLength { set; get; } Member of System.Web.UI.WebControls.TextBox
<asp:TextBox Id="textbox" TextMode="MultiLine" MaxLength="255" runat="server" Columns="25" Rows="5"></asp:TextBox>
@GeorgeGreat i have edited my answer and tested it my self, please have a look. with onKeyPress="return(this.value.length < 255);
1

You should use $('#textbox').html instead of $('#textbox').value. $('#textbox').html takes the content between the textbox tags. Because a textbox does not has a value attribute.

Comments

1

You need to use

$("#<%= textbox.ClientID %>").val("myValue")

aspnet renames the textbox ID's to make sure there are no duplicates on the client side, so textbox can become something like PlaceHolder1_textbox. And then your hardcoded javscript reference will not work anymore.

2 Comments

It says that the syntax is wrong. I must put as well ClientID="static"?
Using ClientIDMode="Static" is not recommended. It can lead to duplicate id's.
-1

When you define your text your have to specified ClientIDMode="Static". In this way .NET create a input box with your name. Usually an input box name is more complex than the name you chose.

Another way is to use a bit of code in the javascript. Instead of $('#textbox') you have to use

$('#<%= yourtextbox.CliendId%>')

2 Comments

At least make sure the code is correct... $('#<%= yourtextbox.ClientID %>')
Yes. You have to use it in an ASP.NET page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.