Make HTML5 input type="number" accepting dashes

To allow HTML5 input fields to accept dashes along with numbers, you cannot use type="number" directly since it only accepts numeric characters. Instead, use type="text" with a pattern attribute to validate the input format.

The Problem with type="number"

HTML5 input type="number" strictly accepts only numeric values and will reject any non-numeric characters including dashes. To accept dashes, we need to use type="text" with pattern validation.

Solution Using Pattern Attribute

Use the pattern attribute with a regular expression to validate numbers with dashes:

<!DOCTYPE html>
<html>
<head>
    <title>Number Input with Dashes</title>
</head>
<body>
    <form>
        <label for="phone">Phone Number:</label>
        <input type="text" 
               pattern="[0-9]+([-][0-9]+)*" 
               name="phone" 
               title="Numbers with dashes (e.g., 123-456-7890)"
               placeholder="123-456-7890"
               required>
        <button type="submit">Submit</button>
    </form>
</body>
</html>

Pattern Explanation

The regular expression [0-9]+([-][0-9]+)* breaks down as:

  • [0-9]+ - One or more digits at the start
  • ([-][0-9]+)* - Zero or more groups of dash followed by digits
  • [-] - A literal dash character
  • [0-9]+ - One or more digits after the dash

Example with JavaScript Validation

<!DOCTYPE html>
<html>
<head>
    <title>Number Input Validation</title>
</head>
<body>
    <form id="numberForm">
        <input type="text" 
               id="numberInput" 
               pattern="[0-9]+([-][0-9]+)*" 
               placeholder="Enter number with dashes">
        <button type="submit">Validate</button>
        <p id="result"></p>
    </form>

    <script>
        document.getElementById('numberForm').addEventListener('submit', function(e) {
            e.preventDefault();
            const input = document.getElementById('numberInput');
            const result = document.getElementById('result');
            
            if (input.checkValidity()) {
                result.textContent = 'Valid format: ' + input.value;
                result.style.color = 'green';
            } else {
                result.textContent = 'Invalid format. Use numbers with dashes (e.g., 123-456)';
                result.style.color = 'red';
            }
        });
    </script>
</body>
</html>

Alternative Pattern for Different Formats

For more flexible patterns, you can modify the regular expression:

// For numbers with dashes or commas
pattern="[0-9]+([-,][0-9]+)*"

// For phone numbers (US format)
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"

// For social security numbers
pattern="[0-9]{3}-[0-9]{2}-[0-9]{4}"

Key Points

  • Use type="text" instead of type="number" for dash support
  • The pattern attribute provides client-side validation
  • Always include a descriptive title attribute for user guidance
  • JavaScript validation can provide immediate feedback

Conclusion

To accept dashes in number inputs, use type="text" with a pattern attribute containing the appropriate regular expression. This approach maintains validation while allowing the desired format flexibility.

Updated on: 2026-03-15T23:18:59+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements