r/learnjavascript • • 4d ago

Simple Password input field

Hi all, I've been learning webcoding recently and Ive been wanting to make a website to use as a prop in my tabletop games.

My biggest desire right now is to do a password input where submitting an incorrect input displays an error message, while the correct input provides a redirect to a new page.

Since this will only be for my friends and wont host any private data, I'm not too concerned for heavy security, but most resources online are for actual websites and online security. Whats the best way for me to implement something like this?

5 Upvotes

11 comments sorted by

4

u/Discordbothost 4d ago

since it's just a prop for friends you can do the whole thing in the browser, no server needed. roughly:

<form id="f">
  <input type="password" id="pw">
  <button>enter</button>
  <p id="err" hidden>wrong password</p>
</form>
<script>
document.getElementById('f').addEventListener('submit', e => {
  e.preventDefault()
  if (document.getElementById('pw').value === 'dragon') {
    location.href = 'vault.html'
  } else {
    document.getElementById('err').hidden = false
  }
})
</script>

the preventDefault is the bit that trips most people up first, without it the form just reloads the page. anyone who opens devtools can read the password (and the vault.html url), but for a tabletop prop that's fine, a player digging it out is kind of in the spirit of the game anyway

1

u/Successful-Throat621 3d ago

thank you so much!!! yeah my players tend to play with in the spirit of the game so hopefully nobody looks at the code lol

2

u/fckueve_ 3d ago

You can hash the password, so even if they will look into code, they won't be able to get the password.

2

u/efari_ 3d ago edited 3d ago

Man, the boilerplate you decide to use just to prevent the form submission from reloading the page.

Instead of getting the DOM element, adding an event listener and then preventDefault, You could just add “action” or even “onSubmit” param to the form to call a js function

2

u/TinyNiceWolf 3d ago

As another option, you might consider basic authentication. This is secure (as long as your site uses https) and requires no javascript code, just two config files to set it up.

1

u/Successful-Throat621 3d ago

oo i'll look into this! ty

1

u/Both-Tart7460 3d ago

ke a really cool tabletop game prop.

Since this is just for friends and you don't need real backend databases, here is the cleanest Vanilla HTML & JS way to do it:

```html

<form id="puzzleForm">

<input type="password" id="passInput" placeholder="Enter secret code..." required autofocus />

<button type="submit">Unlock</button>

</form>

<p id="errorMsg" style="color: #ff5555; display: none;">❌ Incorrect passcode! Try again.</p>

<script> const SECRET_CODE = "dragon123"; // your secret password const form = document.getElementById("puzzleForm"); const input = document.getElementById("passInput"); const errorMsg = document.getElementById("errorMsg"); form.addEventListener("submit", (e) => { // 1. Crucial: prevents browser from refreshing immediately! e.preventDefault(); // 2. trim() and toLowerCase() prevents annoying space / capital errors on mobile if (input.value.trim().toLowerCase() === SECRET_CODE.toLowerCase()) { errorMsg.style.display = "none"; // Redirect to the secret victory page window.location.href = "secret_room.html"; } else { errorMsg.style.display = "block"; input.value = ""; // clear input for next attempt input.focus(); } }); </script>

```

Two quick tips that saved me headaches when learning:

- Always use `e.preventDefault()`, otherwise submitting a form refreshes the page and your error message disappears in 0.1 second!

- Using `.trim()` ensures players on phones don't get rejected just because their keyboard added a sneaky space at the end.

Have fun with your tabletop session!

1

u/Successful-Throat621 3d ago

I think the formatting got a little messed up when you pasted this into reddit? I tried putting in my code but i cant seem to clean it up into a way that works

1

u/Fluffy-Salary-6998 2d ago

If you do not want the password in the source at all, use it as the page name. Name the secret page `dragon.html` and do `fetch(pw + '.html', {method: 'HEAD'})`, if response is ok then `location.href = pw + '.html'`, if not show the error. Now there is nothing to find in devtools, not the password and not the url, wrong guess is just a 404. Works on static hosting, but not when you open the file from disk, fetch needs http. Hashing hides the password but the vault url still sits in the code.

1

u/Alternative-Host-210 19h ago

About the example you couldn't get running: its script has been squeezed onto one line, including comments starting with //. In JavaScript, // comments out everything until the next line break, so the first comment can accidentally hide the rest of that script.

Start with the shorter example above that uses a form submit listener. Keep the script after the form, and put each statement on its own line. Test a wrong word first, then the correct word, then submitting with Enter.

If it still fails, paste your current HTML and the first red error from the browser console here. That will make it much easier to identify the exact problem than adding another full version of the code.