jQuery在模糊上验证文本字段
问题描述:
我正在使用jQuery Validation插件: http://docs.jquery.com/Plugins/验证/
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>
我认为使用onfocusout选项会起作用,但它仍然在用户输入时进行验证。
我做错了什么?
I thought using the onfocusout option would work but it's still validating as the user types. What am I doing wrong?
谢谢
答
您可以使用此代码验证onblur:
You can use this code to validate onblur:
$("#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
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>