如何确保javascript将在主浏览器上运行

时间:2014-02-12 09:29:16

标签: javascript firefox

我有以下JavaScript代码验证登录表单,如果有效则提交。代码适用于chrome和IE 但在Firefox上,它不会在passwordinfousernameinfodocument.getElementById("passwordinfo").innerText = "please enter password";)上显示错误反馈 请帮助我找到问题,因为我要添加ajax,这让我很害怕。

<!DOCTYPE html>
<html>
    <head>
    <link href="mystyle.css" rel="stylesheet" type="text/css"> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
    <title>welcome</title>
    <script type="text/javascript">
    function isvalidmail(mail) {
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return filter.test(mail);
    }

    function checklogin() {
        var email = document.getElementById("username").value;
        var password = document.getElementById("password").value;
        if(email.length < 1 || email.length > 30||(!isvalidmail(email)))
            document.getElementById("usernameinfo").innerText = "please enter valid email";
        else {
            document.getElementById("usernameinfo").innerText = "";
            if(password.length === 0) 
                document.getElementById("passwordinfo").innerText = "please enter password";
            else {
                document.getElementById("passwordinfo").innerText = "";
                document.forms["login"].submit();
            }
        }
    }
    </script>
    </head>
    <body onload="checkCookies()">
        <script>
            function checkCookies(){
                if (!navigator.cookieEnabled==true){
                    document.write("You need to enable cookies to make the site work properly.<br>Please          enable cookies and <a href='index.php'>Reload</a>");
                }
            }
        </script>
        <div id="container">
            <div id="headerpart">
                <?php include "module_header.php" ?>
            </div>
            <div id="bodypart">
                <div id="nav">      
                    <?php 
                    include 'navigation_bar.php';
                    ?>
                </div>
                <div id="mainop">
                    <span id="sessionexp"><?php if(isset($_GET["redirect"]))if($_GET["redirect"]=='session') echo         "Your Session has Expired please login"; ?></span>
                    <form id="login" action="checklogin.php" method="POST" align="center">
                        <table border="0"  bgcolor="#FFFFCB" align="center"><br/>
                            <thead>please login below</thead>
                            <tr>
                                <td>email:</td>
                                <td><input type="text" id="username" name="username"  /></td>
                                <td>
                                    <span class="warning" id="usernameinfo" name="usernameinfo">
                                        <?php
                                        if(isset($_GET["errno"]) && $_GET["errno"] == 1)
                                        echo "wrong username."
                                        ?>
                                    </span>
                                </td>
                            </tr>
                            <tr>
                                <td>password:</td>
                                <td><input type="password" id="password" name="password" /></td>
                                <td>
                                    <span class="warning" id="passwordinfo" name="passwordinfo">
                                        <?php
                                        if(isset($_GET["errno"]) && $_GET["errno"] == 2)
                                        echo "wrong password.";
                                        ?>
                                    </span>
                                </td>
                            </tr>
                        </table>
                        <br />

                        <input type="button" value="Login" onclick="checklogin()"/>
                        <?php echo " New User?  <a href=\"register.php\"><b>Register freely!</b></a>"; ?>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>

3 个答案:

答案 0 :(得分:1)

使用,

document.getElementById('passwordinfo').textContent=''; //For firefox

或尝试

document.getElementById('passwordinfo').innerHTML='';

有关详情,请点击HERE

答案 1 :(得分:1)

firefox不支持

innerText。而是尝试innerHTML。如果有效,请告诉我。

答案 2 :(得分:0)

您还可以使用html attribut reguiredexplain)支持最主要的浏览器(仅限Safari)来检查用户输入。 对于电子邮件验证,您可以使用type=email属性(explain)来验证与所需attribut具有相同支持的电子邮件输入。