admin管理员组

文章数量:1334365

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?

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?

Share Improve this question edited Sep 8, 2015 at 10:34 Amir Iqbal 88112 silver badges28 bronze badges asked Jul 14, 2014 at 10:23 Mohammad Areeb SiddiquiMohammad Areeb Siddiqui 10.2k16 gold badges73 silver badges118 bronze badges 4
  • Your question says that you want it for HTML5; but you have used the javascript tag... – hjpotter92 Commented Jul 14, 2014 at 12:59
  • "Let's use a regex" is not the first thing that es to mind here. – Jongware Commented Jul 14, 2014 at 13:48
  • When you say "Doesn't need to be" do you mean "Is not"? – AndrewPolland Commented Jul 14, 2014 at 13:56
  • stackoverflow./a/50633979/10511266 might help. – Hussnain Haidar Commented Sep 24, 2019 at 14:29
Add a ment  | 

3 Answers 3

Reset to default 4

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>

I'm not great at regex but I can't think of anyway of doing the second and third conditions without disallowing every bination 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.

    <!-- 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>

本文标签: javascriptHTLM5 Pattern for PAK Phone Number FormatStack Overflow