Use fictional values only. Obfuscation is not encryption: values remain available to scripts and form submission. The event example logs state changes, not values. If JavaScript or the module fails, fields remain readable and editable without obfuscation.
Basic Obfuscation
The simplest usage: wrap a form field with
form-obfuscator to obfuscate its value when not
focused. Focus the field to edit the fictional reference,
then Tab away to mask it. Without JavaScript, the native
field stays readable and editable.
<form-obfuscator>
<label for="basic-example">
Account reference
</label>
<input
type="text"
id="basic-example"
name="reference"
value="DEMO-1234"
/>
</form-obfuscator>
Custom Characters
Use the character attribute to specify which
character to use for obfuscation. The default is
*.
<form-obfuscator character="•">
<label for="bullet-example">
Bullet Obfuscation
</label>
<input
type="text"
id="bullet-example"
/>
</form-obfuscator>
<form-obfuscator character="X">
<label for="x-example">
X Obfuscation
</label>
<input
type="text"
id="x-example"
/>
</form-obfuscator>
Pattern-Based Obfuscation
Set pattern="\d{4}$" to keep the last four
digits visible and mask the rest. If the value doesn’t
match, the component masks the whole value. Use fictional
numbers here.
<form-obfuscator pattern="\d{4}$">
<label for="ssn-example">
SSN (shows last 4 digits)
</label>
<input
type="text"
id="ssn-example"
placeholder="123-45-6789"
/>
</form-obfuscator>
<form-obfuscator pattern="\d{4}$">
<label for="phone-example">
Phone (shows last 4 digits)
</label>
<input
type="text"
id="phone-example"
placeholder="(555) 123-4567"
/>
</form-obfuscator>
Maximum Length
Use the maxlength attribute to limit how many
characters are displayed when obfuscated.
<form-obfuscator maxlength="4">
<label for="max-example">
Long Value (max 4 chars shown)
</label>
<input
type="text"
id="max-example"
value="DEMO-REFERENCE-123456789"
/>
</form-obfuscator>
Custom Replacer Function
Set replacer to trusted JavaScript function
body text, not a function reference. It requires
pattern. This body calls an authored global
helper with arguments[0], the matched text, to
mask every other character. A restrictive Content Security
Policy can block compilation; see the
configuration caveats.
<script>
window.customReplacer = (match) => {
return match.split('').map((char, i) =>
i % 2 === 0 ? char : '▪'
).join('');
};
</script>
<form-obfuscator
pattern=".*"
replacer="return customReplacer(arguments[0])">
<label for="replacer-example">
Custom Replacer
</label>
<input
type="text"
id="replacer-example"
value="HelloWorld"
/>
</form-obfuscator>
Advanced Combinations
Combine a pattern with a trusted replacer body or a display limit to keep useful parts of a fictional value visible.
Credit Card with Custom Replacer
Mask the digits before the last group while keeping hyphens in place.
<script>
function cardNumberReplacer() {
var beginning = arguments[0][1];
var final_digits = arguments[0][2];
return beginning.replace(/\d/g, '*')
+ final_digits;
}
</script>
<form-obfuscator
pattern="^((?:[\d]+\-)+)(\d+)$"
replacer="return cardNumberReplacer(arguments)">
<label for="cc-replacer">
Credit Card
</label>
<input
type="text"
id="cc-replacer"
name="cc-replacer"
value="1234-5678-9012-3456"
/>
</form-obfuscator>
Email with Custom Replacer
Mask the username while leaving the
@ and domain visible.
<script>
function emailReplacer() {
var username = arguments[0][1];
var domain = arguments[0][2];
return username.replace(/./g, '*')
+ domain;
}
</script>
<form-obfuscator
pattern="^(.*?)(@.+)$"
replacer="return emailReplacer(arguments)">
<label for="email-replacer">
Email Address
</label>
<input
type="text"
id="email-replacer"
name="email-replacer"
value="user@example.com"
/>
</form-obfuscator>
Pattern + Character + Maxlength
Keep the last four digits, with bullet masking filling the rest of a 16-character display. The input value is not shortened.
<form-obfuscator
pattern="\d{4}$"
character="•"
maxlength="16">
<label for="combo-example">
Credit Card (limited display)
</label>
<input
type="text"
id="combo-example"
name="combo-example"
placeholder="1234-5678-9012-3456"
/>
</form-obfuscator>
Real-World Example
A complete form demonstrating multiple obfuscated fields working together, using fictional account details. Each input’s name moves to a hidden input for submission. Without JavaScript, the visible inputs keep their names and values.
<form>
<form-obfuscator>
<label for="account-pwd">
Account reference
</label>
<input
type="text"
id="account-pwd"
name="reference"
value="DEMO-1234"
/>
</form-obfuscator>
<form-obfuscator
pattern="\d{4}$">
<label for="account-ssn">
SSN (last 4 visible)
</label>
<input
type="text"
id="account-ssn"
name="ssn"
value="123-45-6789"
/>
</form-obfuscator>
<form-obfuscator
pattern="\d{4}$"
character="•">
<label for="account-cc">
Credit Card (last 4 visible)
</label>
<input
type="text"
id="account-cc"
name="credit_card"
value="1234-5678-9012-3456"
/>
</form-obfuscator>
<button type="submit">
Submit
</button>
</form>
Event Handling
The component dispatches
form-obfuscator:hide and
form-obfuscator:reveal events when values are
hidden or shown. This example listens only to its own
instance and logs state changes without logging field
values.
<form-obfuscator id="event-obfuscator">
<label for="event-example">Event Demo</label>
<input type="text" id="event-example" />
</form-obfuscator>
<script>
const obfuscator =
document.getElementById('event-obfuscator');
obfuscator.addEventListener(
'form-obfuscator:hide',
(e) => {
console.log('Obfuscated field:', e.detail.field.id);
}
);
obfuscator.addEventListener(
'form-obfuscator:reveal',
(e) => {
console.log('Revealed field:', e.detail.field.id);
}
);
</script>
API Reference
For complete API documentation, see the project README: