jQuery 的实时电子邮件验证不起作用,我找不到错误

发布于 2024-12-07 18:58:00 字数 1974 浏览 3 评论 0原文

这是代码,我找不到错误。背景图像来自本地目录,与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦归所梦 2024-12-14 18:58:00

尝试替换

if(email != 0)

if(email.length != 0)

Try replacing

if(email != 0)

with

if(email.length != 0)
╰ゝ天使的微笑 2024-12-14 18:58:00

更改:

<div><input type=”email” id=”validate” width=”50”><span id=”validEmail”></span></div>

为:

<div><input type="email" id="validate" width="50"><span id="validEmail"></span></div>

我测试了一下,没问题。

Change:

<div><input type=”email” id=”validate” width=”50”><span id=”validEmail”></span></div>

to:

<div><input type="email" id="validate" width="50"><span id="validEmail"></span></div>

I tested it and it is okay.

小巷里的女流氓 2024-12-14 18:58:00

我让你的代码正常工作。基本上你的输入标签不正确。
您的输入标签是

   <input type=”email” id=”validate” width=”50”><span id=”validEmail”></span>

我将其更改为以下

<input type="email" id="validate" width="50" /><span id="validEmail" ></span>

是工作代码。

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

   <script type="text/javascript" src="JS/jquery-1.3.2.min.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))
                    {
                         alert('Valid Email');
                      //  $("#validEmail").css({ "background-image": "url('/myform/logos/validyes.png')" });

                    } else {

                         alert('InValid Email');
                      //  $("#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>

还有一件事。不要在 keyup 事件上检查电子邮件验证。猜测在模糊事件中检查会更合适。

I made ur code working.Basically ur Input tag was not correct.
your input tag was

   <input type=”email” id=”validate” width=”50”><span id=”validEmail”></span>

i changed it to this

<input type="email" id="validate" width="50" /><span id="validEmail" ></span>

Here's is the working code.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

   <script type="text/javascript" src="JS/jquery-1.3.2.min.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))
                    {
                         alert('Valid Email');
                      //  $("#validEmail").css({ "background-image": "url('/myform/logos/validyes.png')" });

                    } else {

                         alert('InValid Email');
                      //  $("#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>

and one moe thing.dont check email validation on keyup event.Guess checking in blur event will be more appropriate.

叹沉浮 2024-12-14 18:58:00

将愚蠢的引号更改为真正的引号,将您的条件添加到 email.length > > 0,然后你不妨缩短你的代码。重复的东西太多了。

function isValidEmailAddress(emailAddress) {
  return /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/.test(emailAddress);
}

$(function() {
  $("#validate").keyup(function() {
    var bg = "none";
    var email = $("#validate").val();
    if(email.length > 0) {
      bg = isValidEmailAddress(email) 
           ? "url('/myform/logos/validyes.png')"
           : "url('/myform/logos/validno.png')";
    }
    $("#validEmail").css("background-image", bg);
  });
});

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.

function isValidEmailAddress(emailAddress) {
  return /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]+$/.test(emailAddress);
}

$(function() {
  $("#validate").keyup(function() {
    var bg = "none";
    var email = $("#validate").val();
    if(email.length > 0) {
      bg = isValidEmailAddress(email) 
           ? "url('/myform/logos/validyes.png')"
           : "url('/myform/logos/validno.png')";
    }
    $("#validEmail").css("background-image", bg);
  });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文