jeudi 10 juin 2021

(JavaScript) Having trouble trying to get an array to not push any more inputs if the array length is equal to or greater than 1

So I'm trying to make a program that lets the user input a username and a password, and then save those two combined in an array. I also want it to not add any more users if the array length is equal to or greater than 1. That's where I'm having difficulty. I tried to make it so that if the array.length is equal to or greater than 1, then it wouldn't add an account, but for some reason, that wouldn't let the user input any accounts at all. I'm still quite new to JavaScript and I'm very confused on why it won't work.

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>Password Username</title>
    <script src="Q6.js"></script>
</head>
<body>
<h1>Password Login</h1>
<h3 id="text">Register TWO users for the system mainframe</h3>
<p id="usercount">Current Number of Users: </p>
<form id="form">
    <input id="name-input" name="username" placeholder="enter username">
    <input id="password-input" type="password" name="password" placeholder="enter password">
</form>
<button onclick="createUser()">Submit</button>
</body>
</html>

JavaScipt (working script, but doesn't limit user amount):

var username = [];
var password = [];
var account = [];
var usernameInput;
var passwordInput;
var accountDetails;
var regUsers;

function createUser(){
    usernameInput = document.getElementById("name-input").value;
    passwordInput = document.getElementById("password-input").value;
    accountDetails = usernameInput + passwordInput;
    document.getElementById('form').reset();

    if (account.length >= 1){
    document.getElementById("text").textContent = "Log in to your account"
    } else{
    document.getElementById("text").textContent = "Register TWO users for the system mainframe"
    }

    if (account.includes(accountDetails)){
    alert('Login Successful');
    } else{
    username.push(usernameInput);
    password.push(passwordInput);
    account.push(accountDetails);
    alert('User Created Successfully');
    regUsers = username.length;
    document.getElementById("usercount").textContent = "Current Number of Users: " + regUsers;
    console.log(account);
    }
    }

Aucun commentaire:

Enregistrer un commentaire