javascript jQuery 在模糊上验证文本字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12202759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Validate Text Field On Blur
提问by Dave
I'm using the jQuery Validation Plugin : http://docs.jquery.com/Plugins/Validation/
我正在使用 jQuery 验证插件: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?
我认为使用 onfocusout 选项会起作用,但它仍然在用户键入时进行验证。我究竟做错了什么?
Thanks
谢谢
回答by Osama Ahmad
You can use this code to validate onblur:
您可以使用此代码来验证 onblur:
jQuery
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
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>