how to count textbox character and show message

Posted by Shanky11 under Interview Questions on 2/11/2013 | Points: 10 | Views : 1463 | Status : [Member] | Replies : 4
i have a textbox and i wants that on writting into it we can write only 120 characteand and a label should be there for counting




Responses

Posted by: Murugavelmsc on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
TextBox1.Text.Trim().Length

Regards,
Murugavel S
murugavel.sadagopan@gmail.com
http://murugavelmsc.blogspot.in/

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/11/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Limit_number_of_characters_TextBox._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function validateLimit(obj, divID, maxchar) {
objDiv = get_object(divID);
if (this.id) obj = this;
var remaningChar = maxchar - obj.value.length;
if (objDiv){
objDiv.innerHTML = remaningChar + " characters left";
}
if (remaningChar <= 0) {
obj.value = obj.value.substring(maxchar, 0);
if(objDiv) {
objDiv.innerHTML = "0 characters left";
}
return false;
}
else
{ return true; }
}
function get_object(id) {
var object= null;
if (document.layers) {
object = document.layers[id];
} else if (document.all) {
object = document.all[id];
} else if (document.getElementById) {
object = document.getElementById(id);
}
return object;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="lblMsg1">120 characters left</div>
<asp:TextBox ID="TextBox1" runat="server" Height="50px" MaxLength="120"
TextMode="MultiLine" Width="600px" ToolTip="Summary:(120 characters)"
onkeyup="return validateLimit(this, 'lblMsg1', 120)"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" Display="Dynamic"
SetFocusOnError="True">*</asp:RequiredFieldValidator>
<br /><br />

</div>
</form>
</body>
</html>


Thanks
If this post helps you mark it as answer


If this post helps you mark it as answer
Thanks

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vforvijay on: 2/11/2013 [Member] Starter | Points: 25

Up
0
Down
Hi..
You can refer the below link to show balance character count using textbox and label control
http://www.dotnetcode.in/2012/04/display-characters-counter-for.html

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 2/11/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
For windows

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Count_textbox_values
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.MaxLength= 120;
lblCount.Text = "Total characters are: " + textBox1.Text.Length;
}
}
}


If this post helps you mark it as answer
Thanks

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response