restrict the "textfield" to accept only digits and maximum 10 digits in magento form

restrict the "textfield" to accept only digits and maximum 10 digits in magento form

September 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
restrict the "textfield" to accept only digits and maximum 10 digits in magento form

Restricting Telephone Field Input in Magento Forms

In Magento, you might want to restrict the telephone field of your customer address form to only accept digits and limit it to a maximum of 10 characters. This is especially useful when you require customers to enter their phone numbers accurately.

Understanding the Issue

The provided code snippet creates a standard text input field for the telephone attribute:

<div class="field">
    <label for="telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label>
    <div class="input-box">
        <input type="text" name="telephone" value="<?php echo $this->escapeHtml($this->getAddress()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text  <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="telephone" />
    </div>
</div>

However, by default, this field allows any character input and doesn’t enforce a maximum length.

Solving the Problem

To achieve your desired behavior—allowing only digits and limiting to 10 characters—you can modify the field type and add some JavaScript validation. Here’s how:

  1. Change the Field Type: Change the type attribute of the input field from text to tel. This doesn’t restrict the input but makes the browser expect a phone number.

  2. Add Inputmask: Use an external library like Inputmask to enforce the desired format on the client-side. First, download and enable the library in your theme.

  3. Modify the JavaScript: Add the following script to restrict the input length:

require(['jquery', 'mage/cookies'], function($) {
    $('#telephone').on('input', function() {
        var value = $(this).val();
        if (value.length > 10) {
            alert('The telephone number cannot exceed 10 digits.');
            $(this).val(value.substring(0, 10));
        }
    });
});

This script listens for input changes and alerts the user if they try to enter more than 10 digits. It then truncates the value back to 10 characters.

Conclusion

By combining these techniques—changing the field type and adding client-side validation with Inputmask and custom JavaScript—you can effectively restrict the telephone field in your Magento forms to accept only 10 digits, enhancing the data integrity of your customer information.

Remember that while this solution validates on the front-end, you should always validate user input on the back-end as well for security purposes. For more advanced validation needs, consider using Zend_Validate in your controller or model.

Don’t forget to test thoroughly after implementing these changes to ensure they work correctly with your specific theme and Magento version. If you need further assistance with Magento development, feel free to explore our Magento services at LaravelCompany.

API Customers Errors JavaScript

Our network