jQuery 的实时电子邮件验证不起作用,我找不到错误
这是代码,我找不到错误。背景图像来自本地目录,与 jQuery 库相同。我已经测试了这些路径,它们都很好。不知怎的,有些东西不起作用,我无法找出答案。
<html>
<head>
<script type="text/javascript" src="/myform/js/jquery.js"></script>
<script type="text/javascript">
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
return pattern.test(emailAddress);
}
$(document).ready(function() {
$("#validate").keyup(function(){
var email = $("#validate").val();
if(email != 0)
{
if(isValidEmailAddress(email))
{
$("#validEmail").css({ "background-image": "url('/myform/logos/validyes.png')" });
} else {
$("#validEmail").css({ "background-image": "url('/myform/logos/validno.png')" });
}
} else {
$("#validEmail").css({ "background-image": "none" });
}
});
});
</script>
<style>
#validEmail
{
margin-top: 4px;
margin-left: 9px;
position: absolute;
width: 16px;
height: 16px;
}
</style>
</head>
<body>
<div><input type=”email” id=”validate” width=”50”><span id=”validEmail”></span></div>
</body>
</html>
here is the code, I can not find the bug. The background images are coming from a local directory, the same with jQuery library. I have tested the paths and they are all fine. Somehow something is not working and I can not find out.
<html>
<head>
<script type="text/javascript" src="/myform/js/jquery.js"></script>
<script type="text/javascript">
function isValidEmailAddress(emailAddress) {
var pattern = new RegExp(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/);
return pattern.test(emailAddress);
}
$(document).ready(function() {
$("#validate").keyup(function(){
var email = $("#validate").val();
if(email != 0)
{
if(isValidEmailAddress(email))
{
$("#validEmail").css({ "background-image": "url('/myform/logos/validyes.png')" });
} else {
$("#validEmail").css({ "background-image": "url('/myform/logos/validno.png')" });
}
} else {
$("#validEmail").css({ "background-image": "none" });
}
});
});
</script>
<style>
#validEmail
{
margin-top: 4px;
margin-left: 9px;
position: absolute;
width: 16px;
height: 16px;
}
</style>
</head>
<body>
<div><input type=”email” id=”validate” width=”50”><span id=”validEmail”></span></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试替换
为
Try replacing
with
更改:
为:
我测试了一下,没问题。
Change:
to:
I tested it and it is okay.
我让你的代码正常工作。基本上你的输入标签不正确。
您的输入标签是
我将其更改为以下
是工作代码。
还有一件事。不要在 keyup 事件上检查电子邮件验证。猜测在模糊事件中检查会更合适。
I made ur code working.Basically ur Input tag was not correct.
your input tag was
i changed it to this
Here's is the working code.
and one moe thing.dont check email validation on keyup event.Guess checking in blur event will be more appropriate.
将愚蠢的引号更改为真正的引号,将您的条件添加到
email.length > > 0
,然后你不妨缩短你的代码。重复的东西太多了。Change the goofy quotes, to real quotes, add swap out your condition to
email.length > 0
, and then you might as well shorten your code. Too much stuff repeated.