jQuery Validate Text Field On Blur

别等时光非礼了梦想. 提交于 2019-12-08 12:02:20

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/12202759/jquery-validate-text-field-on-blur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!