Total counts 100. After I entered 101 letter show alerts or error message it is not working in this code.
Html
<asp:TextBox ID="txtarea" runat="server" Width="700px" Height="80px" TextMode="MultiLine"
onkeyup="cnt(this)" MaxLength="100"></asp:TextBox>
<p style="text-align: right;">
<asp:Label ID="lblcharcnt" runat="server" Text="100"></asp:Label>
</p>
<asp:Label ID="lblcount" runat="server" Text='<%#Eval("row") %>' Visible="false"></asp:Label>
JavaScript
<script type="text/javascript">
function cnt(text) {
var a = text.value;
var b = "character left.";
text.parentNode.getElementsByTagName('span')[0].innerHTML = 100 - a.length + " " + b;
}
</script>
Total counts 100. After I entered 101 letter show alerts or error message it is not working in this code.
Html
<asp:TextBox ID="txtarea" runat="server" Width="700px" Height="80px" TextMode="MultiLine"
onkeyup="cnt(this)" MaxLength="100"></asp:TextBox>
<p style="text-align: right;">
<asp:Label ID="lblcharcnt" runat="server" Text="100"></asp:Label>
</p>
<asp:Label ID="lblcount" runat="server" Text='<%#Eval("row") %>' Visible="false"></asp:Label>
JavaScript
<script type="text/javascript">
function cnt(text) {
var a = text.value;
var b = "character left.";
text.parentNode.getElementsByTagName('span')[0].innerHTML = 100 - a.length + " " + b;
}
</script>
Share
Improve this question
edited Jun 21, 2018 at 14:26
halfer
20.4k19 gold badges109 silver badges202 bronze badges
asked May 10, 2017 at 6:43
Ivin RajIvin Raj
3,4312 gold badges33 silver badges70 bronze badges
1
- 1 pure jquery jsfiddle/umynof8b – Mark Commented May 10, 2017 at 7:05
6 Answers
Reset to default 2Did changes in javascript. Try it and let me know if further help require.
function cnt(text) {
var a = text.value;
var b = "character left.";
if (a.length > 100) {
alert('length grater than 100.');
} else {
text.parentNode.getElementsByTagName('span')[0].innerHTML = 100 - a.length + " " + b;
}
}
This is the code I use for this purpose. Note the use of <%= 1000 - TextBox1.Text.Length %>
. This will make sure the correct remaining characters are displayed after PostBack or setting an initial value.
<asp:TextBox ID="TextBox1" onKeyUp="setMaxLength(this)" isMaxLength="1000" runat="server" TextMode="MultiLine"></asp:TextBox>
<span id="<%=TextBox1.ClientID %>_remain"><%= 1000 - TextBox1.Text.Length %></span>
<script type="text/javascript">
function setMaxLength(control) {
//get the isMaxLength attribute
var mLength = control.getAttribute ? parseInt(control.getAttribute("isMaxLength")) : ""
//was the attribute found and the length is more than the max then trim it
if (control.getAttribute && control.value.length > mLength) {
control.value = control.value.substring(0, mLength);
alert('Length exceeded');
}
//display the remaining characters
var modid = control.getAttribute("id") + "_remain";
if (document.getElementById(modid) != null) {
document.getElementById(modid).innerHTML = mLength - control.value.length;
}
}
</script>
This will show a popup if you eneter more than 100 characters in textbox
ASP.Net
<asp:TextBox ID="txtQ1F0" runat="server" TextMode="MultiLine" Rows="2" Columns="35" onKeyUp="javascript:Count(this);" onChange="javascript:Count(this);" />
javascript
function Count(text) {
var maxlength = 100; //set your value here
var object = document.getElementById(text.id)
if (object.value.length > maxlength) {
object.focus(); //set focus to prevent jumping
var count1=object.value.length;
alert('You have exceeded the ment length of 100 characters , total characters entered are : '+count1);
object.value = text.value.substring(0, maxlength); //truncate the value
object.scrollTop = object.scrollHeight; //scroll to the end to prevent jumping
return false;
}
return true;
}
I have rewritten JavaScript function like below. Now, User can not enter post maximum length. User will be restricted to enter only 100 characters
function multilineTextBoxKeyDown(textBox, e, maxLength) {
var selectedText = textBox.value;
var b = "character left.";
if (!checkSpecialKeys(e)) {
var length = parseInt(maxLength);
if (textBox.value.length > length) {
textBox.value = textBox.value.substring(0, maxLength);
textBox.parentNode.getElementsByTagName('span')[0].innerHTML = maxLength - textBox.value.length + " " + b;
alert('Maximum no of characters reached'); //go on with your own ment
}
else {
textBox.parentNode.getElementsByTagName('span')[0].innerHTML = maxLength - textBox.value.length + " " + b;
}
}
else {
//Below code shows how many characters left on deleting the text
textBox.parentNode.getElementsByTagName('span')[0].innerHTML = maxLength - textBox.value.length + " " + b;
}
}
function checkSpecialKeys(e) {
if (e.keyCode != 8 && e.keyCode != 9 && e.keyCode != 33 && e.keyCode != 34 && e.keyCode != 35 && e.keyCode != 36 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40 && e.keyCode != 46) {
return false;
} else {
return true;
}
}
Also, call JavaScript like below
<asp:TextBox ID="txtarea" runat="server" Width="700px" Height="80px" TextMode="MultiLine" onkeyup="multilineTextBoxKeyDown(this,event,'100')" MaxLength="100"></asp:TextBox>
<p style="text-align: right;">
<asp:Label ID="lblcharcnt" runat="server" Text="100"></asp:Label>
Code is tested now. Also, remaining number of characters will be shown on deleting the text from textbox
Hope it helps
Html:
<asp:TextBox ID="yourid" runat="server" maxlength="100" ></asp:TextBox>
Script:
$("#yourid").keyup(function ()
{
var a=$("#yourid").val();
if(a.length>99)
{
alert("error");
}
});
It will help you, Try this,
Javascrtipt
$("#textboxId").keyup(function ()
{
var Textboxvalue = $("#textboxId").val();
if (Textboxvalue .length > 100)
{
alert("Should be 100 characters only");
}
});
Html
<asp:TextBox ID="textboxId" runat="server" MaxLength="100"></asp:TextBox>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744558549a4580741.html
评论列表(0条)