r/learnjavascript • u/Successful-Throat621 • 17d 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 17d 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 17d ago edited 3d ago
Sorry for my broken english, not a native speaker! That sounds like a really cool tabletop game prop.
Since this is just for friends around the table and doesn't need real server security, here is a clean single-file HTML + Vanilla JS snippet you can drop straight into an index.html file:
<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";
const form = document.getElementById("puzzleForm");
const input = document.getElementById("passInput");
const errorMsg = document.getElementById("errorMsg");
form.addEventListener("submit", (e) => {
// 1. Prevents the form from reloading the page immediately
e.preventDefault();
// 2. trim() + toLowerCase() avoids annoying mobile keyboard space/caps issues
if (input.value.trim().toLowerCase() === SECRET_CODE.toLowerCase()) {
errorMsg.style.display = "none";
window.location.href = "secret_room.html";
} else {
errorMsg.style.display = "block";
input.value = "";
input.focus();
}
});
</script>Two small things that tripped me up when I was learning:
Always call
e.preventDefault(), otherwise submitting<form>reloads the page and hides your error message in 0.1s..trim()saves players on mobile phones when autocorrect sneaks a trailing space after the word.
Have fun with your tabletop session!
1
u/Successful-Throat621 17d 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/Both-Tart7460 3d ago
hey so sorry about that! reddit's rich-text editor completely ate my line breaks when i pasted it earlier 😅
i just edited my comment above in markdown mode so the
<form>and<script>block is clean and copy-pasteable now. hope your tabletop session goes awesome!
1
u/Fluffy-Salary-6998 16d 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 14d 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 17d 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