logo dile-components dile-components

dile-input-number-mask

A web component that creates a customized numeric input field with automatic formatting applied via a mask pattern. The component separates the user-editable value (clean numbers) from the displayed value (formatted with mask characters).

<dile-input-number-mask 
  label="Phone" 
  placeholder="Type your telephone" 
  mask="00 000-00-00"
></dile-input-number-mask>

Install

npm install @dile/ui

Usage

Import the component:

import '@dile/ui/components/input/input-number-mask.js';

Use it in your HTML:

<dile-input-number-mask
  name="phone"
  label="Phone Number"
  placeholder="Enter your number"
  mask="00 000-00-00"
></dile-input-number-mask>

Properties

Inherited Properties

This component extends dile-input, so it inherits many useful properties:

Events

const input = document.querySelector('dile-input-number-mask');

input.addEventListener('enter-pressed', () => {
  console.log('User pressed Enter');
  console.log('Value (without mask):', input.value);
});

input.addEventListener('input', (e) => {
  console.log('Current masked display:', input.maskedValue);
  console.log('Current raw value:', input.value);
});

Behavior & Features

Smart Cursor Positioning

The component automatically maintains intuitive cursor positioning:

Input Validation

Only numeric characters (0-9) are allowed:

Mask Examples

Mask Input maskedValue value Use Case
00 000-00-00 121234567 12 123-45-67 121234567 Phone (10 digits)
0000 0000 0000 0000 1111222233334444 1111 2222 3333 4444 1111222233334444 Credit card
000-00-0000 1234567890 123-45-6789 1234567890 US SSN format
+00 (000) 000-00-00 121234567890 +12 (123) 456-78-90 121234567890 International phone

Common Use Cases

Basic Phone Input with Validation

<script type="module">
import '@dile/ui/components/input/input-number-mask.js';
</script>
<dile-input-number-mask 
  name="phone"
  label="Phone Number" 
  placeholder="Enter your 10-digit phone number" 
  mask="00 000-00-00"
></dile-input-number-mask>

Credit Card with Error Handling

<dile-input-number-mask 
  name="card"
  label="Credit Card Number"
  placeholder="16-digit card number"
  mask="0000 0000 0000 0000"
  message="Enter a valid 16-digit card number"
></dile-input-number-mask>

Pre-filled Value

<dile-input-number-mask 
  label="Account Number" 
  value="1234567890"
  mask="00 000-00-00"
></dile-input-number-mask>

Disabled State

<dile-input-number-mask 
  label="Read-only Account" 
  value="121234567"
  mask="00 000-00-00"
  disabled
></dile-input-number-mask>

With Form Submission

const form = document.querySelector('form');
const phoneInput = form.querySelector('dile-input-number-mask');

form.addEventListener('submit', (e) => {
  e.preventDefault();
  
  // Get the clean numeric value for your backend
  const phoneNumber = phoneInput.value;
  console.log('Sending to backend:', phoneNumber); // e.g., "121234567"
  
  // The maskedValue is only for display
  console.log('User sees:', phoneInput.maskedValue); // e.g., "12 123-45-67"
});

CSS Customization

Inherit all CSS custom properties from dile-input:

dile-input-number-mask {
  --dile-input-label-color: #333;
  --dile-input-border-color: #ccc;
  --dile-input-border-width: 2px;
  --dile-input-focus-ring-color: rgba(102, 170, 255, 0.5);
  --dile-input-error-border-color: #c00;
  --dile-input-padding: 8px;
  --dile-input-font-size: 1.1em;
}

Methods

const input = document.querySelector('dile-input-number-mask');

// Focus the input
input.focus();

// Clear error after validation
input.errored = false;
input.message = '';
// Or use the helper method
input.clearError();