1

I'm pretty much new to jquery. I've been trying to do validations where i check if a textbox length is greater than 4.

So, this is what i did:

   $(document).ready(function () {

    $('#tbstreet1').blur(isValid);
                        });
   function isValid()
{
    Boolean isvalid;
   var street1Length = $("#tbstreet1").val().length;

   alert(street1Length);
     if(myLength >4)
     {
         isvalid= true;
        ActivateSave()
     }


}
function ActivateSave()
    {

   $("#btn_save").attr("Enabled",true);
   }

This is my aspx page:

   <asp:Label runat="server" ID="street1" Text="Street1" O></asp:Label> 
  <asp:TextBox ID="tbstreet1" runat="server" CausesValidation="true"  ></asp:TextBox>

 <asp:Button ID="btn_save" runat="server" Text="Save" Enabled="false" />

I even have script source in my .net page

 <script src="http://code.jquery.com/jquery-1.6.4.js"  type="text/javascript" > 
 </script>

But the thing has been that how do i call the javascript function from here? as i 'm pretty sure that my javascript is not getting called. Can u help me?

3 Answers 3

3

You need to 'attach' your jQuery function to your textbox:

$('#tbstreet1').blur(isValid);

This will run the isValid function when the text field is blurred. Alternatively, you can provide an anonymous function:

$('#tbstreet1').blur(function() {
    // do something, maybe even 'isValid();'
});

This is good if the code you need to run is one-off, but if you need the function to be executed multiple times in multiple places, it's better to create a 'named' function, like in the first example.

Keep in mind that you need to put all this code inside the onReady function, which will execute only after the page is finished loading (and the DOM is ready for navigation). You can do this by simply using:

<script type="text/javascript">
    // this is a shortcut for $(document).ready()
    $(function() {
        // your code
    });
</script>
Sign up to request clarification or add additional context in comments.

13 Comments

Don't forget that needs to be in a .load() or .ready() handler. Or at least called after that element in the page load.
Heh Jared, I was editing my answer when you posted that comment :).
Hey I did this .. I'll just update my code in a second but still it doesn't work for me..so can u please help me
My guess is that it's the <asp:TextBox ID="tbstreet1" runat="server" CausesValidation="true"></asp:TextBox>. I don't use ASP.NET myself, so you'll have to look at what HTML is being created, and which element the tbstreet1 ID is being set on. jQuery can only act on HTML elements. You may need to do something like $('#tbstreet1 input').blur(isValid);.
k... Let me check the source and test it...give me a miute
|
0

just to add to @Daniel T.'s answer:

The blur event is sent to an element when it loses focus, to have a more accurate text change event you can use this plugin

I suggest you to use firebug to make js debug, it makes things much easier

Comments

0

Also, if you havent made the change, if you are running your textbox at server you do need

 $('#<%= tbstreet1.ClientID %>').blur(function(){});

instead of

 $('#tbstreet1').blur(function(){});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.