How to create a mobile number input box with fixed country code

3 min read 22-09-2024
How to create a mobile number input box with fixed country code


Creating a user-friendly mobile number input box is essential for enhancing user experience on forms, especially when dealing with international clients. This guide will walk you through the process of creating a mobile number input field with a fixed country code, ensuring that users only need to enter their phone number without having to select their country code every time.

Problem Scenario

Imagine you're developing a contact form for your website and need to collect users' mobile numbers. You want to include a pre-set country code (e.g., +1 for the United States) to streamline the process and minimize input errors. Below is an original code example that can be improved for this purpose:

<input type="tel" name="mobile" placeholder="Your mobile number">

Improved Mobile Number Input with Fixed Country Code

Step 1: Set Up the HTML Structure

We will modify the original input code to include a fixed country code. Here’s how it can look:

<div>
    <label for="country-code">Country Code:</label>
    <span id="country-code">+1</span>
    <input type="tel" id="mobile-number" name="mobile" placeholder="Your mobile number" required>
</div>

Explanation of the Code

  1. Country Code Display: We use a <span> element to display the fixed country code (+1 in this case).
  2. Mobile Number Input: The <input> field is designated for the user's mobile number, marked with a tel type for appropriate mobile input on devices.

Step 2: Styling the Input Box

To make the input box visually appealing and user-friendly, you can apply some CSS styles:

div {
    display: flex;
    align-items: center;
}

#country-code {
    margin-right: 5px;
    font-weight: bold;
}

input[type="tel"] {
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    width: 250px; /* Adjust width as needed */
}

Step 3: Adding Validation

It’s essential to validate the mobile number format. You can enhance user experience by utilizing JavaScript for real-time validation:

document.getElementById('mobile-number').addEventListener('input', function (e) {
    const value = e.target.value;
    // Simple validation for a 10-digit number after the country code
    if (!/^\d{10}$/.test(value)) {
        e.target.setCustomValidity('Please enter a 10-digit mobile number.');
    } else {
        e.target.setCustomValidity('');
    }
});

Practical Example

Now that we’ve created a structure, here’s a complete example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mobile Number Input</title>
    <style>
        /* Styles as outlined above */
    </style>
</head>
<body>
    <form>
        <div>
            <label for="country-code">Country Code:</label>
            <span id="country-code">+1</span>
            <input type="tel" id="mobile-number" name="mobile" placeholder="Your mobile number" required>
        </div>
        <input type="submit" value="Submit">
    </form>
    <script>
        // Validation code as outlined above
    </script>
</body>
</html>

Conclusion

Creating a mobile number input box with a fixed country code is straightforward and improves user experience on your site. By following the steps outlined in this article, you will have a functional and appealing input field that prompts users to enter their mobile numbers while maintaining a consistent format.

Additional Resources

By implementing this input field, you can ensure that users find it easy to provide their contact information, leading to a more effective form submission process.