登录表单会在输入错误信息时立即刷新页面

时间:2017-12-14 19:18:24

标签: javascript html

我目前正在尝试进行注册和登录页面,到目前为止一切正常,除了一件事,当我尝试登录时,如果我使用完整的电子邮件地址页面立即刷新页面,但如果我输入电子邮件错误,例如" test",它会告诉我这是错误的。但如果我尝试输入正确的电子邮件" test@test.com"如果输入错误的密码,它只会刷新页面。到目前为止,我有这个编码。

<div id="mainsection">
    <div id="loginPara">
    <form id="usrDetails" onsubmit = "return false()">
        Enter Email address:<br>  
        <input type="email" id="emailInput" required><br>
        Enter Password:<br>
        <input type="password" id="passwordInput" require><br>
        <input type="submit" onclick="login()">
    </form>
    </div>

<p id="loginFailure" style="color:red"></p>
</div>
<script>
    function checkLogin(){
        if(localStorage.loggedInUsrEmail !== undefined){
            //extract details of logged in user
            var usrObj = JSON.parse(localStorage[localStorage.loggedInUsrEmail]);
            //say hello to logged in user
            document.getElementById("loginPara").innerHTML = usrObj.firstName + " logged in.";
        }
    }

    function login(){
        //get email address
        var email = document.getElementById("emailInput").value;

        //user does not have an account
        if(localStorage[email] === undefined){
            //inform user that they do not have an account
            document.getElementById("loginFailure").innerHTML = "email not recognized. do you have an account?";
            return false; //do nothing else
        }
        else {//user has an account
            var usbObj = JSON.parse(localStorage[email]);//convert to object
            var password = document.getElementById("passwordInput").value;
            if(password === usbObj.password){//successful login
            document.getElementById("loginPara").innerHTML= usbObj.firstName + " logged in."
            document.getElementById("loginFailure").innerHTML = "";
            localStorage.loggedInUsrEmail = usbObj.email;
            }
            else{document.getElementById("loginFailure").innerHTML = "Password not correct, please try again.";
             }   
        }
    }

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

这是无效的

<form id="usrDetails" onsubmit = "return false()">

如果没有括号(),则需要返回false。