代码错误 - 会话和本地存储

时间:2017-06-26 21:41:23

标签: javascript ecmascript-5

我为我的网站制作了会话和本地存储的代码。虽然它似乎不起作用,我不知道是什么问题。我尝试了一切使它工作,但我做不到。问题是您可以注册本地和会话存储可以保存您的详细信息,但我无法登录。登录按钮始终处于禁用状态。

HTML CODE:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="logreg.js"></script>
</head>
<body>
<script>
    document.write(outstate(isative()));
</script>
    <form>
        <input type="email" id="user" onblur="verlogin()" required>
        <input type="password" id="pass" onblur="verlogin()" required>
        <span id="mess"></span>
        <button  type="submit" disabled id="login" onclick="login();">LOGIN</button>
    </form>

        <input type="email" id="usermail" onblur="veruser();ch();" required>
        <span id="veruser"></span>
        <button style="text-decoration: none!important;display: none;" id="uservalue" value="0"></button>
        <input type="password" id="password" onblur="verpass();ch();" required>
        <input type="password" id="confirmpassword" onblur="verpass();ch();" required>
        <span id="verpass"></span>
        <button style="text-decoration: none!important;display: none;" id="passvalue" value="0"></button>
        <input type="text" id="morada" required>
        <input type="number" maxlength="9" id="nif"  required>
        <button type="submit" id="registar" onclick="registar();">REGISTER</button>




</body>
</html>

JAVASCRIPT CODE:

window.onload=function(){
    if(sessionStorage.length==0){
        var key='';
        for(var i=0;i<localStorage.length;i++){
            key = localStorage.key(i);
            sessionStorage.setItem(key,'');
        }
    }
}

function verlogin(){
    var nome=document.getElementById('user').value;
    var pass=document.getElementById('pass').value;
    document.getElementById('mess').innerHTML = '';
    var key='';
    var val='';
    if(localStorage.length!=0){
        for(var i=0;i<localStorage.length;i++){
            key = localStorage.key(i);
            val = localStorage.getItem(key);
            if(nome!='' || pass!=''){
                if(nome==key){
                    if(val.substr(0, val.indexOf('|'))==pass) {
                        document.getElementById('mess').style.color='green';
                        document.getElementById('mess').innerHTML = 'VALID';
                        document.getElementById("login").disabled = false;

                    }else{
                        document.getElementById("login").disabled = true;

                        document.getElementById('mess').style.color='red';
                        document.getElementById('mess').innerHTML = 'User or Wrong Password';
                    }
                }else{
                    document.getElementById("login").disabled = true;
                    document.getElementById('mess').style.color='red';
                    document.getElementById('mess').innerHTML = 'User or Wrong Password';
                }
            }else{
                document.getElementById('mess').innerHTML = '';

            }
        }
    }else{
        document.getElementById("login").disabled = true;
        document.getElementById('mess').style.color='red';
        document.getElementById('mess').innerHTML = 'Create an Account';
    }

}

function login(){
    var nome=document.getElementById('user').value;

    sessionStorage.setItem(nome,'ativo');
    window.location.href='index.html';
}

function veruser(){
    var nome=document.getElementById('usermail').value;
    document.getElementById('uservalue').value = 0;
    var key='';
    if(localStorage.length!=0) {
        for (var i = 0; i < localStorage.length; i++) {
            key = localStorage.key(i);
            if (nome != '') {
                if (nome == key) {
                    document.getElementById('uservalue').value = 0;
                    document.getElementById('veruser').style.color = 'red';
                    document.getElementById('veruser').innerHTML = 'User already in use';
                } else {
                    document.getElementById('uservalue').value = 1;

                    document.getElementById('veruser').style.color = 'green';
                    document.getElementById('veruser').innerHTML = 'Valid';

                }
            } else {
                document.getElementById('veruser').innerHTML = '';

            }
        }
    }else{
        document.getElementById('uservalue').value = 1;

        document.getElementById('veruser').style.color = 'green';
        document.getElementById('veruser').innerHTML = 'Valid';
    }
}
function ch(){
    var verp = document.getElementById('passvalue').value;
    var veru = document.getElementById('uservalue').value;
    document.getElementById("login").disabled = true;

    if(verp==1 && veru==1){
        document.getElementById("registar").disabled = false;
    }else{
        document.getElementById("login").disabled = true;

    }

}
function verpass(){
    var pass1=document.getElementById('password').value;
    var pass2=document.getElementById('confirmpassword').value;
    document.getElementById('passvalue').value = 0;
    if(pass1!='' || pass2!=''){
        if(pass1!=pass2){
            document.getElementById('passvalue').value = 0;
            document.getElementById('verpass').style.color='red';
            document.getElementById('verpass').innerHTML = 'Passwords don't match';
        }else{
            document.getElementById('passvalue').value = 1;
            document.getElementById('verpass').style.color='green';
            document.getElementById('verpass').innerHTML = 'Valid';
        }
    }else{
        document.getElementById('veruser').innerHTML = '';

    }
}

function registar(){
    var verp = document.getElementById('passvalue').value;
    var veru = document.getElementById('uservalue').value;
    var user=document.getElementById('usermail').value;
    var pass=document.getElementById('password').value;
    var mor=document.getElementById('morada').value;
    var nif=document.getElementById('nif').value;
    if(verp=='1' && veru=='1'){
        localStorage.setItem(user,pass + '|'+mor);
        sessionStorage.setItem(user,'ativo');
        window.location.href='index.html';

    }else{
        alert('ERRO');
    }
}

function logout(){
    var key='';
    var val='';
    for(var i=0;i<sessionStorage.length;i++){
        key = sessionStorage.key(i);
        val = sessionStorage.getItem(key);
        if(val=='ativo'){
            sessionStorage.setItem(key,'');
            window.location.href='index.html';
        }
    }
}
function isative(){
    var key='';
    var val='';
    var user='';
    for(var i=0;i<sessionStorage.length;i++){
        key = sessionStorage.key(i);
        val = sessionStorage.getItem(key);
        if(val=='ativo'){
            user = key;
        }else{
            user=0;
        }
    }
    return user;
}

function outstate(ative){
    var content='';
    if(ative!=0){
        alert(ative);
        content+= '<h6>The user ' + ative+' is online<h6>';
        content +='<button type="submit" onclick="logout();">LOGOUT</button>'
    }else{
        content +='<h6>Not Connected<h6>';
    }
    return content
}

我不知道该怎么办...... :(

0 个答案:

没有答案