Anu
Anu

Reputation: 3

Display message when I finish typing certain characters in textbox

I have a requirement where I need to display a message beside Password text box that Limit is only 15 characters. When user enters 15 characters, a blinking label should be displayed immediately which will alert user.

I achieved it when user clicks outside textbox, but I need to display message as soon as user types 15th character

While typing in the text box itself if max limit reached the user should be prompted.

My Code:

<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();
        }
        });
    });
}

Upvotes: 0

Views: 1701

Answers (4)

Anu
Anu

Reputation: 3

I have achieved this following way. Hope this may help someone

aspx code:

<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 fucntion:

This function will recieve blink message as parameter and calls a blink function

 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; 
    }
}

This function blinks the text using 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"); });
 }

This is used to hide message when user clears character(less than 15). This checks for backspace and delete key press:

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

Upvotes: 0

Jai
Jai

Reputation: 269

so your problem is just to display message or so as soon as when the character count of the textbox reaches 15..

use onkeyup/onkeydown/onkeypress event handler according to your requirement in the textbox control.

try this

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

Upvotes: 0

Baga
Baga

Reputation: 1442

Here is one way of doing it.

    $(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();
            }
        });
    });

Upvotes: 0

Manoj B. Kalla
Manoj B. Kalla

Reputation: 5

Copy your textbox "password" value into variable in Jquery and get the length and check it.

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>

Another Very good answer of your question.

Upvotes: 0

Related Questions