0

I created a textbox in ASP.NET which when you click on the save button, the text will be saved in the database. In the database however, I specified a limit for the text which is 20 characters. I simply want to change the border color to red when the user writes more than 20 characters in this TextBox. Anyway to do this without using javascript, instead use C#?

Using ASP.NET Web Forms Web Site

1
  • On blur event Ajax call to c# with text box value and on error change style to red. Commented Aug 1, 2015 at 18:22

2 Answers 2

1

Anyway to do this without using javascript, instead use C#?

Only by doing a postback on every character. This is likely to make typing very hard: all those page refreshes are likely to cause characters to be lost.

JavaScript is definitely the right way to do something like this.

Sign up to request clarification or add additional context in comments.

2 Comments

I think I have no choice but to use javascript...Can you please explain what I need to write please?
@Jack Look at the input event of the control in the DOM. You could use this event to change the CSS class of the control. There are plenty of examples of this kind of thing.
0

Why not just put a max length of 20 characters on the textbox instead? Then the user cannot enter more than 20 characters.

<input type="text" maxlength="20" />

or

<asp:TextBox runat="server" MaxLength="20" />

2 Comments

Yes I ultimately did that...But I faced another problem which was with my Multiline textbox, it seems that maxlentgh doesn't work with it.
stackoverflow.com/questions/1334286/… has a solution for working around multiline maxlength if you haven't seen it yet.

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.