JavaScript中的文本框最小长度验证
总数为100。在我输入101字母显示警报或错误消息后,它在此代码中不起作用 HtmlJavaScript中的文本框最小长度验证,javascript,c#,jquery,asp.net,Javascript,C#,Jquery,Asp.net,总数为100。在我输入101字母显示警报或错误消息后,它在此代码中不起作用 Html JavaScript <script type="text/javascript"> function cnt(text) { var a = text.value; var b = "character left."; text.parentNode.getElementsByTagName('span')[0].innerHTML
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>
功能cnt(文本){
var a=text.value;
var b=“字符左。”;
text.parentNode.getElementsByTagName('span')[0].innerHTML=100-a.length+“”+b;
}
如果您在文本框中输入超过100个字符,将显示一个弹出窗口ASP.Net
<asp:TextBox ID="txtQ1F0" runat="server" TextMode="MultiLine" Rows="2" Columns="35" onKeyUp="javascript:Count(this);" onChange="javascript:Count(this);" />
如果您在文本框中输入超过100个字符,将显示一个弹出窗口
ASP.Net
<asp:TextBox ID="txtQ1F0" runat="server" TextMode="MultiLine" Rows="2" Columns="35" onKeyUp="javascript:Count(this);" onChange="javascript:Count(this);" />
这是我用于此目的的代码。注意
的用法。这将确保回发或设置初始值后显示正确的剩余字符
<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>
函数setMaxLength(控件){
//获取isMaxLength属性
var mlLength=control.getAttribute?parseInt(control.getAttribute(“isMaxLength”):“”
//是否找到属性且长度超过最大值,然后修剪它
if(control.getAttribute&&control.value.length>mllength){
control.value=control.value.substring(0,最大长度);
警报(“超出长度”);
}
//显示其余字符
var modid=control.getAttribute(“id”)+“_remaine”;
if(document.getElementById(modid)!=null){
document.getElementById(modid).innerHTML=mlLength-control.value.length;
}
}
这是我用于此目的的代码。注意
的用法。这将确保回发或设置初始值后显示正确的剩余字符
<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>
函数setMaxLength(控件){
//获取isMaxLength属性
var mlLength=control.getAttribute?parseInt(control.getAttribute(“isMaxLength”):“”
//是否找到属性且长度超过最大值,然后修剪它
if(control.getAttribute&&control.value.length>mllength){
control.value=control.value.substring(0,最大长度);
警报(“超出长度”);
}
//显示其余字符
var modid=control.getAttribute(“id”)+“_remaine”;
if(document.getElementById(modid)!=null){
document.getElementById(modid).innerHTML=mlLength-control.value.length;
}
}
对javascript进行了更改。试试看,如果需要进一步帮助,请告诉我
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;
}
}
在javascript中进行了更改。试试看,如果需要进一步帮助,请告诉我
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;
}
}
我已经重写了JavaScript函数,如下所示。现在,用户不能输入post最大长度。用户只能输入100个字符
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 comment
}
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;
}
}
另外,调用JavaScript,如下所示
<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>
代码现在正在测试中。另外,在从文本框中删除文本时,将显示剩余的字符数我已经重写了如下JavaScript函数。现在,用户不能输入post最大长度。用户只能输入100个字符
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 comment
}
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;
}
}
另外,调用JavaScript,如下所示
<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>
代码现在正在测试中。另外,从文本框中删除文本时将显示剩余的字符数,希望有帮助
Html:
希望能有帮助
Html:
它会帮助你,试试这个 Javascrtipt
$("#textboxId").keyup(function ()
{
var Textboxvalue = $("#textboxId").val();
if (Textboxvalue .length > 100)
{
alert("Should be 100 characters only");
}
});
Html
它会帮你的,试试这个 Javascrtipt
$("#textboxId").keyup(function ()
{
var Textboxvalue = $("#textboxId").val();
if (Textboxvalue .length > 100)
{
alert("Should be 100 characters only");
}
});
Html
pure jquery pure jquery谢谢Sir@InnovaITve解决方案谢谢Sir@InnovaITve解决方案谢谢Sir@vdwwd谢谢Sir@VDWWD@IvinRaj,如果它回答了您的问题,请将其标记为answer@IvinRaj,如果回答了您的问题,请将其标记为答案