当我在文本框中输入某些字符时显示消息



我有一个要求,我需要在密码文本框旁边显示一条消息,限制只有15个字符。当用户输入15个字符时,应立即显示一个闪烁的标签,提醒用户。

当用户单击文本框外时我实现了它,但是我需要在用户键入第15个字符时立即显示消息

在输入文本框本身时,如果达到最大限制,则应提示用户

我的代码:

<asp:TextBox runat="server" MaxLength="15" TextMode="Password" ID="Password"></asp:TextBox>
<asp:Label runat="server" AssociatedControlID="Password" ID="lblMaxChar" />
<asp:CustomValidator ID="cvMaxLimit" runat="server" ControlToValidate="Password" ClientValidationFunction="ValidateTextLength" /><br/>
function ValidateTextLength(source, args) {
    var length = $get('<%=Password.ClientID %>').value.length;
    if (length >= 15) {
        var lbl = document.getElementById('<%=lblMaxChar.ClientID %>');
        $(lbl).text('Limit is 15 characters');
       blink(lbl);
    }
}
var stopBlinking = false;
setTimeout(function () {
    stopBlinking = true;
}, 8000);
function blink(selector) {
    $(selector).fadeOut('slow', function () {
        $(this).fadeIn('slow', function () {
           if (!stopBlinking){
                blink(this);
            }
           else {
            $(this).hide();
        }
        });
    });
}

所以你的问题只是显示消息或当文本框的字符数达到15..

根据文本框控件的要求使用onkeyup/onkeydown/onkeypress事件处理程序。

试试这个

> <asp:TextBox runat="server" MaxLength="15" onkeypress="return jQuery/Javascript function();" > TextMode="Password" ID="Password"></asp:TextBox>

将你的文本框"password"值复制到Jquery的变量中,并获得长度并检查它

I submitting example code :
    example:
 <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$(function () {
         $('#btngettext').click(function (e)
                { var GetValue = $('#txtname').val();
                    alert(GetValue.length);
                });
            });
        });
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="txtname"></asp:TextBox>
<asp:Button runat="server" ID="btngettext" Text="Display" />
</div>
</form>
</body>
</html>

这里有一种方法。

    $(document).ready(function () {
        $('#<%=Password.ClientID %>').keydown(function (event) {
            var length = $('#<%=Password.ClientID %>').val().length;
            if (length >= 15) {
                alert('Max 15 characters reached'); //replace this required function call
                event.preventDefault();
            }
        });
    });

我是这样做到的。希望这能帮助到一些人

aspx代码:

<asp:TextBox runat="server" Width="250px" MaxLength="15" TextMode="Password" ID="Password"></asp:TextBox>
<asp:Label runat="server" AssociatedControlID="Password" ID="lblMaxChar" class="blinkingLimitMsg"/>

aspx.cs

 protected void Page_Load(object sender, EventArgs e)
 {   
    Password.Attributes.Add("onkeypress", "return ValidatePasswordLength(myresxKey);
 }
javascript因数:

该函数将接收blink消息作为参数并调用blink函数

 function ValidatePasswordLength(MsgText) {
    var length = $('#<%=Password.ClientID %>').val().length;
    if (length >= 15) {
        var lblMsg = document.getElementById('<%=lblMaxChar.ClientID %>');
        $(lblMsg).text(MsgText);
         BlinkCartItemCount('.blinkingLimitMsg');
         document.onkeydown = KeyCheck; 
    }
}

这个函数使用FadeTo()使文本闪烁:

  function BlinkItem(cssClass) {
  $(cssClass).fadeTo(400, 0.13).fadeTo(400, 1).fadeTo(400, 0.13).fadeTo(400, 1).fadeTo(400, 0.13).fadeTo(400, 1);
  $(cssClass).fadeIn(function () { this.style.removeAttribute("filter"); });
 }

用于在用户清除字符(小于15)时隐藏消息。检查退格和删除键:

 function KeyCheck() 
 {
  var hl = $(".blinkingLimitMsg");
  var KeyID = event.keyCode;
  switch(KeyID)
  {
   case 8: $(hl).text(""); 
           break; 
   case 46: $(hl).text("");
           break;
   default:break;
   }
  }

最新更新