0

I am a new programer in js and I tried to do a basic website using js and html. I tried to do like username and password input boxes but the function of it doesn't work. What is the problem?

This is the code:

    function entering() {
    	alert("hi")
    	var username = document.getElementById("fuser");
    	var password = document.getElementById("fpass");
    	if (username == "f") {
    	if (password == "f") {
    	alert('Nice');
    	}else {
    	alert('wrong password');
    	}else{
    	alert('wrong username');}}
    
    	document.getElementById("frm1").submit();
    }
    <h1>Best Website</h1>
    <h2>Hello!<h2>
    		
    <form id = frm1>
    	Username: <input type = "text" id="fuser"><br>
    	Password: <input type = "text" id="fpass"><br>
    	<input type = "button" onclick = "entering()" value = "submit">
    
    	
    </form>

0

3 Answers 3

4

What you are searching for is the value property of the input element.

var username = document.getElementById("fuser");
var password = document.getElementById("fpass");

Are only references to the elements in your page, not what the user entered, for that use

var username = document.getElementById("fuser").value;
var password = document.getElementById("fpass").value;

Complete Code:

function entering() {
    alert("hi")
    var username = document.getElementById("fuser").value;
    var password = document.getElementById("fpass").value;
    if (username == "f") {
      if (password == "f") {
        alert("correct");
      } else {
        alert("wrong password")
      }
    } else {
      alert("wrong username");
    }

    //document.getElementById("frm1").submit();
}
<h2>Hello!<h2>

<form id = frm1>
    Username: <input type = "text" id="fuser"><br>
    Password: <input type = "text" id="fpass"><br>
    <input type = "button" onclick = "entering()" value = "submit">


</form>

Sign up to request clarification or add additional context in comments.

1 Comment

Your if statements also were a bit messed up, corrected that too.
0
var username = document.getElementById("fuser");
var password = document.getElementById("fpass");

When grabbing an input element, you get the element stored into a variable, not the value. When you write your if conditional then, you need to point to the property on the element called value.

if (username.value == "f") {
if (password.value == "f") {

Comments

0

You'll need to rework your if/else statements and make sure to access the value of the element you're referencing.

if (username == "f") {
if (password == "f") {
alert('Nice');
}else {
alert('wrong password');
}else{
alert('wrong username');}}

To:

if (username.value == "f") {
    if (password.value == "f") {
        alert('Nice');
    } else {
        alert('wrong password');
    }
} else {
    alert('wrong username');
 }

That should get you moving again.

<!DOCTYPE html>
<html>

<head>
    <title>The Best Website</title>
</head>

<body>

    <h1>Best Website</h1>
    <h2>Hello!
        <h2>

            <form id=frm1>
                Username:
                <input type="text" id="fuser">
                <br> Password:
                <input type="text" id="fpass">
                <br>
                <input type="button" onclick="entering()" value="submit">


            </form>

            <script>
                function entering() {
                    alert("hi")
                    var username = document.getElementById("fuser");
                    var password = document.getElementById("fpass");
                    if (username == "f") {
                        if (password == "f") {
                            alert('Nice');
                        } else {
                            alert('wrong password');

                        }
                    } else {
                        alert('wrong username');
                    }

                    document.getElementById("frm1").submit();
                }

            </script>

</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.