Created
August 4, 2025 13:54
-
-
Save pdaug/129a524866821a0aa95eb9606f3f7fca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input | |
id={id} | |
name={name} | |
type="text" | |
value={value} | |
disabled={disabled} | |
required={required} | |
readOnly={readOnly} | |
placeholder={placeholder} | |
onSelect={function (event) { | |
event.currentTarget.setSelectionRange( | |
value.length || 0, | |
value.length || 0, | |
); | |
return; | |
}} | |
onChange={function () { | |
return; | |
}} | |
onKeyDown={function (event) { | |
event.currentTarget.setSelectionRange( | |
value.length || 0, | |
value.length || 0, | |
); | |
if (event.key === "ArrowLeft" || event.key === "ArrowRight") { | |
event.preventDefault(); | |
return; | |
} | |
const currentIndex = value.length; | |
const currentMaskChar = mask[currentIndex]; | |
const nextMaskChart = mask[currentIndex + 1]; | |
// cloning value | |
let newValue = `${value}`; | |
// if backspace | |
if ( | |
event.key === "Del" || | |
event.key === "Delete" || | |
event.key === "Backspace" | |
) { | |
newValue = newValue.slice(0, -1); | |
setValue(newValue); | |
return; | |
} | |
// add letter | |
if ( | |
event.key.length === 1 && | |
currentMaskChar === "A" && | |
/[a-zA-Z]{1}/.test(event.key) | |
) { | |
newValue += event.key; | |
setValue(newValue); | |
} | |
// add number | |
if ( | |
event.key.length === 1 && | |
currentMaskChar === "9" && | |
/[0-9]{1}/.test(event.key) | |
) { | |
newValue += event.key; | |
setValue(newValue); | |
} | |
// add letter or number | |
if ( | |
event.key.length === 1 && | |
currentMaskChar === "#" && | |
/[a-zA-Z0-9]{1}/.test(event.key) | |
) { | |
newValue += event.key; | |
setValue(newValue); | |
} | |
// if next is mask other character | |
if ( | |
nextMaskChart !== "A" && | |
nextMaskChart !== "9" && | |
["-", "_", "."].includes(nextMaskChart) | |
) { | |
newValue += nextMaskChart; | |
setValue(newValue); | |
} | |
return; | |
}} | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment