Skip to content

Instantly share code, notes, and snippets.

@av1d
Created April 25, 2025 16:13
Show Gist options
  • Save av1d/a8dbb2afde8ce258382a7f85166779c6 to your computer and use it in GitHub Desktop.
Save av1d/a8dbb2afde8ce258382a7f85166779c6 to your computer and use it in GitHub Desktop.
Screen Reader Accessible PIN-Entry Scrambling Keypad
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Accessible Scramblepad Keypad</title>
<style>
body {
font-family: Arial, sans-serif;
background: #f4f4f4;
display: flex; flex-direction: column; align-items: center; margin-top: 40px;
}
.pad-container {
width: 320px;
max-width: 95vw;
}
#output {
width: 100%;
height: 44px;
font-size: 1.5em;
margin-bottom: 20px;
padding: 5px 10px;
border: 2px solid #888;
border-radius: 6px;
background: #fff;
letter-spacing: 6px;
text-align: left;
box-sizing: border-box;
}
#keypad {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: repeat(4, 56px);
grid-gap: 10px;
width: 100%;
}
.key {
font-size: 1.4em;
border-radius: 8px;
border: 1px solid #bbb;
background: #e0e0e0;
cursor: pointer;
transition: background 0.15s;
user-select: none;
width: 100%;
height: 100%;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
}
.key:active {
background: #b0c4de;
}
.enter-key {
grid-column: 1 / span 3;
background: #4caf50;
color: #fff;
font-weight: bold;
font-size: 1.3em;
border: 2px solid #388e3c;
}
.enter-key:active {
background: #388e3c;
}
@media (max-width: 400px) {
.pad-container { width: 99vw; }
#output, #keypad { font-size: 1.1em; }
.key, .enter-key { font-size: 1.1em; }
#keypad { grid-template-rows: repeat(4, 44px); }
}
</style>
</head>
<body>
<div class="pad-container">
<fieldset>
<legend id="pad-legend">PIN Entry Keypad</legend>
<!-- ARIA live region for screen reader feedback -->
<input
id="output"
type="text"
readonly
aria-label="PIN entry"
aria-describedby="pad-legend"
role="status"
aria-live="polite"
placeholder="Enter PIN"
/>
<div id="keypad" aria-label="PIN keypad" role="group"></div>
</fieldset>
</div>
<script>
const output = document.getElementById('output');
const keypad = document.getElementById('keypad');
let digits = ['1','2','3','4','5','6','7','8','9','0'];
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
function renderKeypad() {
keypad.innerHTML = '';
for (let i = 0; i < 9; i++) {
const btn = document.createElement('button');
btn.textContent = digits[i];
btn.className = 'key';
btn.type = 'button';
btn.setAttribute('aria-label', `Digit ${digits[i]}`);
btn.onclick = () => handleDigit(digits[i]);
keypad.appendChild(btn);
}
// Last row: 0, Clear, Back
const btn0 = document.createElement('button');
btn0.textContent = digits[9];
btn0.className = 'key';
btn0.type = 'button';
btn0.setAttribute('aria-label', `Digit ${digits[9]}`);
btn0.onclick = () => handleDigit(digits[9]);
keypad.appendChild(btn0);
const btnClear = document.createElement('button');
btnClear.textContent = 'Clear';
btnClear.className = 'key';
btnClear.type = 'button';
btnClear.setAttribute('aria-label', 'Clear all digits');
btnClear.onclick = handleClear;
keypad.appendChild(btnClear);
const btnBack = document.createElement('button');
btnBack.textContent = 'Back';
btnBack.className = 'key';
btnBack.type = 'button';
btnBack.setAttribute('aria-label', 'Delete last digit');
btnBack.onclick = handleBack;
keypad.appendChild(btnBack);
// Enter key (spans all columns)
const btnEnter = document.createElement('button');
btnEnter.textContent = 'Enter';
btnEnter.className = 'key enter-key';
btnEnter.type = 'button';
btnEnter.setAttribute('aria-label', 'Submit PIN');
btnEnter.onclick = handleEnter;
keypad.appendChild(btnEnter);
}
function handleDigit(d) {
output.value += d;
output.setAttribute('aria-live', 'polite');
output.setAttribute('aria-label', `PIN entry, ${output.value.length} digits entered`);
digits = shuffle(digits);
renderKeypad();
}
function handleClear() {
output.value = '';
output.setAttribute('aria-label', 'PIN entry, cleared');
}
function handleBack() {
output.value = output.value.slice(0, -1);
output.setAttribute('aria-label', `PIN entry, ${output.value.length} digits entered`);
}
/* ENTER KEY FUNCTIONS - choose one. */
// popup dialog which displays PIN:
function handleEnter() {
output.setAttribute('aria-label', `PIN entry submitted, ${output.value.length} digits`);
alert('Entered: ' + output.value);
}
/*
// Submit PIN to PHP or other script:
function handleEnter() {
output.setAttribute('aria-label', `PIN entry submitted, ${output.value.length} digits`);
// Redirect to the PHP endpoint with the pin as a query parameter
window.location.href = 'http://example.org/input.php?pin=' + encodeURIComponent(output.value);
}
*/
/*
// Submit PIN via AJAX (without leaving page)
function handleEnter() {
output.setAttribute('aria-label', `PIN entry submitted, ${output.value.length} digits`);
fetch('http://example.org/input.php?pin=' + encodeURIComponent(output.value))
.then(response => response.text())
.then(data => {
// handle PHP response here, e.g., show a message
alert('Server response: ' + data);
});
}
*/
renderKeypad();
</script>
<!--
MIT License
Copyright 2025 av1d
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment