0

I am trying to get my code to check if the value submitted in "username" already exist in "usernames" list. If it does, would like to have the message displayed and if it does not, add that value to the "usernames" list and submit form.

Don't understand why my code doesn't work. Please see below:

HTML

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Validating Form Data</title>
    <link rel="stylesheet" href="index.css" type="text/css">
  </head>
  <body>
      <h3>Register your account:</h3>
    <p id="errors"></p>
    <form id="registration-form" method="POST" action="https://formdump.codeinstitute.net/">
        <div class="username">
            <label for="username">Username:</label>
            <input id="username" name="username" type="text" required>
        </div>
        <div class="password">
            <label for="password">Password:</label>
            <input id="password" name="password" type="password" required>
        </div>
        <input type="submit">
        
    </form>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.js" integrity="sha256-DrT5NfxfbHvMHux31Lkhxg42LY6of8TaYyK50jnxRnM=" crossorigin="anonymous"></script>
    <script src="index.js"></script>
  </body>
</html>

CSS

label {
    display: block;
}
input {
    margin-bottom: 1rem;
}
p {
    color: red;
    font-weight: bold;
}

JS

let usernames = ["Harry", "Daisy", "Michael", "Sarah", "Sally"];

// Write your code here
let form = document.getElementById("registration-form");
form.addEventListener("submit", handleSubmit);

let errorMsg = document.getElementById("errors");
let uName = form.elements["username"].value;

let abc = usernames.includes(uName);

function handleSubmit(event) {
    event.preventDefault();
    if (abc) {
        errorMsg.innerHTML = `<p>Sorry, the username ${uName} is already in use. Please choose another username.</p>
  `;
    } else {
        usernames.push(uName);
        form.submit();
        console.log(usernames);
    }
}

I tried replacing includes() with indexOf(), but still doesn't work.

1
  • Needs to put let uName = form.elements["username"].value;let abc = usernames.includes(uName); inside handleSubmit Commented Nov 19, 2022 at 13:25

1 Answer 1

1

As I said in the comment,you need to get the input value correctly each time it submit,so needs to put below code inside handleSubmit,otherwise uName will be null and abc will always be false(due to it execute before submiting)

let uName = form.elements["username"].value;
let abc = usernames.includes(uName);

let usernames = ["Harry", "Daisy", "Michael", "Sarah", "Sally"];

// Write your code here
let form = document.getElementById("registration-form");
form.addEventListener("submit", handleSubmit);

let errorMsg = document.getElementById("errors");

function handleSubmit(event) {
    event.preventDefault();
    let uName = form.elements["username"].value;
    let abc = usernames.includes(uName);
    if (abc) {
        errorMsg.innerHTML = `<p>Sorry, the username ${uName} is already in use. Please choose another username.</p>
  `;
    } else {
        usernames.push(uName);
        form.submit();
        console.log(usernames);
    }
}
label {
    display: block;
}
input {
    margin-bottom: 1rem;
}
p {
    color: red;
    font-weight: bold;
}
<!DOCTYPE html>
<html lang="en-US">
  <head>
    <title>Validating Form Data</title>
    <link rel="stylesheet" href="index.css" type="text/css">
  </head>
  <body>
      <h3>Register your account:</h3>
    <p id="errors"></p>
    <form id="registration-form" method="POST" action="https://formdump.codeinstitute.net/">
        <div class="username">
            <label for="username">Username:</label>
            <input id="username" name="username" type="text" required>
        </div>
        <div class="password">
            <label for="password">Password:</label>
            <input id="password" name="password" type="password" required>
        </div>
        <input type="submit">
        
    </form>
    <script src="https://code.jquery.com/jquery-3.5.1.slim.js" integrity="sha256-DrT5NfxfbHvMHux31Lkhxg42LY6of8TaYyK50jnxRnM=" crossorigin="anonymous"></script>
    <script src="index.js"></script>
  </body>
</html>

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

1 Comment

@Andy Yeah,I am updating it

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.