Search code examples
javascriptjqueryjquery-validateonblur

jQuery Validate Text Field On Blur


I'm using the jQuery Validation Plugin : http://docs.jquery.com/Plugins/Validation/

I'm having a problem which is causing the validation to start when the user starts typing however this is not what I want. I want to the validation to start after the user leaves the text field. Here is my code:

    <input class="required email" type="text" size="26" name="usernameField" id="usernameField" /> 

<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="http://jzaefferer.github.com/jquery-validation/jquery.validate.js"></script>
<script type="text/javascript">

  $(document).ready(function(){

         $("#usernameField").validate({
           onfocusout:false
          });


   });

</script>

I thought using the onfocusout option would work but it's still validating as the user types. What am I doing wrong?

Thanks


Solution

  • You can use this code to validate onblur:

    jQuery

    $("#Email").blur(function() 
    {
     var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;  
     var emailaddress = $("#Email").val();
     if(!emailReg.test(emailaddress)) 
        $("#emailspan").html('<font color="#cc0000">Please enter valid Email address</font>');  
     else
        $("#emailspan").html('<font color="#cc0000"></font>');  
    });
    

    HTML

    <h4>
    <br />Email <span id="star5">*</span>
    </h4>                       
    <input type="text" id="Email" name="Email" maxLength="50" size="45" /><span id="emailspan" value="0"></span>