C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Count the characters while entering in TextBox using Javascript
WhatsApp
Senthilkumar
5y
37.1
k
0
0
25
Blog
Count the characters while entering in TextBox using Javascript
It explains about the Count the characters while entering in TextBox using Javascript.
Design:
<table cellpadding=
"0"
cellspacing=
"0"
border=
"0"
width=
"100%"
>
<tr>
<td>
<asp:TextBox ID=
"txtComments"
BackColor=
"Gray"
ForeColor=
"White"
runat=
"server"
TextMode=
"MultiLine"
onkeyup=
"return CountCharacters();"
Width=
"400px"
Height=
"75px"
></asp:TextBox>
</td>
</tr>
<tr>
<td style=
"padding-top:10px;"
>
Characters remaining: <asp:TextBox ID=
"txtRemain"
BackColor=
"Gray"
ForeColor=
"White"
runat=
"server"
Text=
"200"
Width=
"45px"
></asp:TextBox>
</td>
</tr>
</table>
JavaScript:
<script type=
"text/javascript"
>
function
CountCharacters()
{
var
maxSize = 200;
if
(document.getElementById(
'<%= txtComments.ClientID %>'
))
{
var
ctrl = document.getElementById(
'<%= txtComments.ClientID %>'
);
var
len = document.getElementById(
'<%= txtComments.ClientID %>'
).value.length;
if
(len <= maxSize) {
var
diff = parseInt(maxSize) - parseInt(len);
if
(document.getElementById(
'<%= txtRemain.ClientID %>'
))
{
document.getElementById(
'<%= txtRemain.ClientID %>'
).value = diff;
}
}
else
{
ctrl.value = ctrl.value.substring(0, maxSize);
}
}
return
false
;
}
</script>
Screenshot:
Count the characters while entering in TextBox using Javascript
People also reading
Membership not found