skip to Main Content

I have small form in php with one input field for mobile number. currently i am entering mobile number in way that it start with 09 and maximum 11 digit . currently i can entered phone number of total 11 digit and if i wrote less than 11 than it successfully show error message. But i want mobile number should be entered in a way that there should b – after first four digit. and rest come after that. for example 0900-12345678 below is my code

<form action="action.php">
<label>Mobile No</label>
<input type="text" id="mobno" name="mobno" pattern="09[0-9]{9}" maxlength="11"><br><br>
<input type="submit" value="Submit">
</form>

2

Answers


  1. I think change your pattern to this:

    <input type="text" id="mobno" name="mobno" pattern="03[0-9]{2}-[0-9]{8}" maxlength="13" required>
    
    
    
    Login or Signup to reply.
  2. The pattern you’re using enforces a mobile number starting with "09" and followed by exactly 9 digits. However, this doesn’t match the format you want (e.g., 0300-12345678), and it doesn’t allow for the inclusion of a – after the first four digits

    You need to use this pattern instead 03[0-9]{2}-[0-9]{8}

    • 03: Specifies that the mobile number should start with "03".
    • [0-9]{2}: Matches exactly two digits after "03".
    • "-": Requires a hyphen after the first four digits.
    • [0-9]{8}: Matches exactly eight digits after the hyphen.

    And you should increase the maxlenght to 13

    <form action="action.php" method="post">
        <label for="mobno">Mobile No</label>
        <input type="text" id="mobno" name="mobno" pattern="03[0-9]{2}-[0-9]{8}" maxlength="13" required>
        <br><br>
        <input type="submit" value="Submit">
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search