简体   繁体   中英

I am linking my login js file(log.js) into this html code,but it does not make any change

This is my .html body content:

<form name="login"> 
    <table border="0" cellpadding="2.0" cellspacing="2.0" align="left"> 
        <tr>
            <td>Username&nbsp;</td>
            <td>
                <input name="username" type="text" id="username" maxlength="15"></input>
            </td> 
        </tr> 
        <tr>
            <td>Password&nbsp;</td>
            <td>
                <input name="password" type="password" id="password" maxlength="10"></input>       
            </td> 
        </tr> 
        <tr>
            <td>&nbsp;</td> 
            <td>&nbsp;</td> 
        </tr> 
        <tr>
            <td>&nbsp;</td>
            <td>
                <strong>
                    <input name="Login" type="button" value="Submit" onclick="Vlogin()"></input>
                </strong>
            </td> 
        </tr> 
    </table> 
</form>

...and javascript code:

function Vlogin()
{
    var user=document.login.username;
    var pass=document.login.password;
    if (user.value=="account1" && pass.value=="backup1") 
    { 
        window.location= "index.jsp";
    }
    else
        alert("Invalid username or password");
        user.focus();
    }
}
function Vlogin()
{
    var user=document.login.username;
    var pass=document.login.password;
    if (user.value == "account1" && pass.value == "backup1") 
    { 
        window.location= "index.jsp";
    }
    else{
        alert("Invalid username or password");
        user.focus();
    }
}

curly bracket after "else"

You can't access elements in your DOM by the method you are using.. such as document.login

If you want to use raw JS, you can use getElementsByTagName() https://developer.mozilla.org/en-US/docs/DOM/document.getElementsByTagName

but I suggest that you use JQuery as it's pretty much the standard


function Vlogin() {
    var login = $('form[name=login]')
    var user= $('[name=username]', login).val()
    var pass= $('[name=password]', login).val()
    if (user === "account1" && pass ==="backup1") { 
        window.location= "index.jsp";
    } else {
        alert("Invalid username or password");
        user.focus();
    }
}

I also want to point out that your validation code can easily be hacked as this will all be in plain english within your source :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM