r/learnjavascript • u/Successful-Throat621 • 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?
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
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.
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:
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