简体   繁体   中英

JavaScript Form Submit Link

I want a webpage to have a username and password box, and has a submit button that takes it to another page, in this case, Google. If the username and/or password is wrong, an alert will pop up saying "Access Denied." If the user forgot to fill out all the required information, then an alert will pop up saying "Please enter all data." This is not happening. Nothing happens when I click the submit button.

HTML:

<html>
<head>
<title>Password Window</title>
<script type="text/javascript" src="password_window.js"></script>
</head>
<body>
<form name="form" onsubmit=" return loginLink()">
Username: <input type="text" name="username" id="username"/><br/>
Password: <input type="password" name="pwd" id="password"/><br/>
<input type="submit" value="Submit" id="submit"/>
</form>
</body>
</html>

JavaScript:

//Username: username123 Password: abc123

function loginLink()
{var username = document.getElementById('username');
 var password = document.getElementByid('password');
 var submit = document.getElementById('submit');

if (username.value="username123")
{
if (password.value="abc123")
{location.href=("href_window.html");}
else
{window.alert("Access Denied");}
}
else
{window.alert("Access Denied");}

var x = document.forms["form"]["fname"].value;
if (x == null || x == "") {
    window.alert("Please enter all data!");
    return false;
}

HELP WITH CODE!!

You have an error:

var password = document.getElementByid('password');
                                    ^---- Should be Id

Another problem:

if (username.value = "username123") {
                   ^--- must be "=="

the same with password.value = "abc123" .

Then you must return false after alerts to prevent form submission.

You also don't have such element in your form document.forms["form"]["fname"] .

Fixed code: http://jsfiddle.net/cu5oyr05/

First of all in this var password = document.getElementByid('password'); the id is written wrongly it should be var password = document.getElementById('password'); . Second, it will be better if you use this:-

{var username = document.getElementById('username').value;
var password = document.getElementByid('password').value;


Third you don't have a field with the name "fname". Fourth you don't need to fetch the value of submit button. Fifth your = in if should be == .

= is for assignment ,use == for comparison

use if (username.value == "username123") not if (username.value = "username123") for comparison

Change

  • 1.

      if (username.value = "username123") { if (password.value="abc123") 
  • 2.

      document.getElementByid('password'); 
  • 3.

      var x = document.forms["form"]["fname"].value; 

To

  • 1.

      if (username.value == "username123")//== for comparison { if (password.value == "abc123") 
  • 2.

      document.getElementById('password'); 
  • 3.

      var x = document.forms["form"].value; 

DEMO

First: You are missing the closing { at the end of the function.

Second: use (document.getElementById('password');) instead of (document.getElementByid('password');).

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