Mohammad Areeb Siddiqui
Mohammad Areeb Siddiqui

Reputation: 10179

HTLM5 Pattern for PAK Phone Number Format

I am trying to make a html5 pattern for Pakistan Phone Number Format which is as follows:

03xx-xxxxxxx

Where x represents a number. Uptil now I have developed this regex:

/03[0-9]{2}-[0-9]{7}/

What I am trying now is to make sure that the next 7 digits after the hyphen:

  1. Aren't the same numbers like: 1111111
  2. Aren't in a continuous manner like: 1234567
  3. Aren't in a reverse manner like: 7654321

I have no idea how to put these checks in the regex. How can I do this?

Upvotes: 1

Views: 8013

Answers (3)

Tabish Zaman
Tabish Zaman

Reputation: 292

    <!-- Pakistani phone number format: +923101234567 pattern="[\+]\d{2}\d{10}"-->
    <!-- For Reg Expression : pattern : [\\+]\\d{2}\\d{10} -->
    <form>
      <label for="country_code">Pak Phone Number</label>
      <input type="text" id="phone_no" name="phone_no" pattern="[\+]\d{2}\d{10}" title="ex: +923101234567"><br><br>
      <input type="submit">
    </form>

Upvotes: 1

Amir Iqbal
Amir Iqbal

Reputation: 881

Here is HTML5 Pattern For pakistani Mobile Numbers

<input type="text" pattern="03[0-9]{2}-(?!1234567)(?!1111111)(?!7654321)[0-9]{7}" name="mobile_number" placeholder="Mobile Number" required>

Upvotes: 4

coPro
coPro

Reputation: 126

I'm not great at regex but I can't think of anyway of doing the second and third conditions without disallowing every combination of numbers in a continuous manner. If you don't mind really ugly regex then this would work for solving all three rules.

03[0-9]{2}-(?!0123456)(?!1234567)(?!2345678)(?!3456789)(?!4567890)(?!0987654)(?!9876543)(?!8765432)(?!7654321)(?!6543210)([0-9])(?!\1{6})[0-9]{6}

Test here.

Now if you can find another way to test the second two conditions then the regex is much simpler.

03[0-9]{2}-([0-9])(?!\1{6})[0-9]{6}

Test here.

Upvotes: 1

Related Questions