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

Jack
  • 7
  • 6

2 Answers2

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.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • I think I have no choice but to use javascript...Can you please explain what I need to write please? – Jack Aug 01 '15 at 16:49
  • @Jack Look at the [`input`](https://developer.mozilla.org/en-US/docs/Web/Events/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. – Richard Aug 01 '15 at 16:52
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" />
John
  • 374
  • 2
  • 6
  • 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. – Jack Aug 01 '15 at 22:01
  • http://stackoverflow.com/questions/1334286/specifying-maxlength-for-multiline-textbox has a solution for working around multiline maxlength if you haven't seen it yet. – John Aug 02 '15 at 00:27