Shalni
Shalni

Reputation: 1027

How to count number of character which is entered in textbox?

I have a label lblCountCharacter with text "4000" and a textbox txtAddNote where users can enter text.

On entering one character in txtAddNote, the label text is decreased by one.

Please help me write a function for this in asp.net using C#.

Upvotes: 5

Views: 5581

Answers (4)

Shyju
Shyju

Reputation: 218732

I would suggest you to use javascript to do this. in the onkeypress event call a javascript function which will check the length of the content of text box and then update the label in the form.

Upvotes: 1

Miyagi Coder
Miyagi Coder

Reputation: 5532

In order to avoid post backs you can use jQuery to determine the length of the text in the text box:

var myLength = $("#myTextbox").val().length;

Upvotes: 4

Curtis
Curtis

Reputation: 103358

I think you can get a better solution to this by using just javascript/jQuery. Using c# is going to involve having to use AJAX to re-render the Label each time.

var characterLimit = 4000
var charLeft = characterLimit - $(".textbox").val().length
$(".label").html(charLeft);

Upvotes: 2

Jeremy Elbourn
Jeremy Elbourn

Reputation: 2690

If you want to update a label with the remaining character count, you would want to use a javascript function. You can add an event handler for a key press on the textbox that updates the text of the label.

You can find more information about capturing the key presses in a textbox here.

Upvotes: 2

Related Questions