Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question Regular Expression

    HI

    I am trying to write a regular expression that should match the following cases

    1. Should start with either + or 00
    2. Should be numbers only and length between 11 - 16 chars (length excluding + and 00)

    Which means the following should be valid:
    +95215487511
    0041587695157




    My following expression does not work fine

    PHP Code:
    <?php
    $reg 
    ="/^[\+|00]{1}[0-9]{11,16}$/";
    $number '065471852956';
    $x preg_match$reg $number);

    var_dump($x); // returns true
    Any help is appreciated

    Thanks

  • #2
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    Any help is appreciated

    PHP Code:
    $reg ="/^(\+|00)[0-9]{11,16}$/"
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    phantom007 (07-30-2013)

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Hi

    Thanks for your reply.

    Can it also check for overall minimum char length should be 12 ?

    I mean something like this

    Code:
    ^((\+|00)[0-9]{11,16}){12,}$
    But the above does not work

    Pls help

  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    Hi

    I'm not good with regx.


    I don't understand, your original code already says minimum 11

    just change it to 12

    PHP Code:
    $reg ="/^(\+|00)[0-9]{12,16}$/"
    or you mean you want another one that is minimum 12.

    minimum 12 to any number

    PHP Code:
    $reg ="/^(\+|00)[0-9]{12,}$/"
    LT
    Last edited by low tech; 07-30-2013 at 11:00 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    phantom007 (07-30-2013)

  • #5
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Sorry for the confusion

    What I want is, apart from case 1 and case 2 (in my initial post) the total char length should be minimum 12.

    So here are the cases

    1. Should start with either + or 00
    2. After p#1, it should have numbers only and length between 11 - 16 chars
    3. Overall length including pt#1 and pt#2 should be minimum 12 chars and max 17 chars


    Which means the following should be valid:
    +95215487511
    0041587695157

    and not validate this:
    +00654718529




    Thanks
    Last edited by phantom007; 07-30-2013 at 11:58 AM.

  • #6
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    Hi

    My last effort and some test results


    fail
    012345678901234567
    ++01234567890
    +12345678901234567

    pass
    0012345678901
    +01234567890
    +1234567890123456

    PHP Code:
    $reg ="/^(\+|00){1}[0-9]{11,16}$/"
    $number '+12345678901234567'
    $x preg_match$reg $number);
    var_dump($x); 
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    phantom007 (07-30-2013)

  • #7
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    hmm, can u make it such that, if there is + then it cannot be followed by zero, and if there are 00 then it cannot have + at beginning. Then I can have what I am actually looking for.

    So the following will be valid:
    +98657451256
    0098741256325

    and following invalid
    +0025421698548
    12345678901

  • #8
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    Hi

    try

    PHP Code:
    $reg ="/^((\+){1}[1-9]{11,16})|((00){1}[^\+][0-9]{11,16})$/"
    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    phantom007 (07-30-2013)

  • #9
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    ummm....doesnt work for the following:

    +12345678901
    0012345678901

    http://leaverou.github.io/regexplained/

  • #10
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 Posts
    Hi

    Yeh I think I'm lost at this point.

    Maybe somebody with more experience can help.

    sorry

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    phantom007 (07-30-2013)

  • #11
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts



    thanks anyways for all your help so far

  • #12
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Hi Again


    I am using the following regex:
    Code:
    /^(?:\+|00)[1-9][0-9]{10,15}$/
    But it does not validate the following two numbers, any idea why?

    Code:
    005374152368
    +4865718575

    Thanks

  • #13
    Regular Coder
    Join Date
    May 2011
    Posts
    242
    Thanks
    1
    Thanked 57 Times in 56 Posts
    Let's take 005374152368 as an input.
    (?:\+|00) matches the '00'
    [1-9] matches the '5'
    [0-9]{10,15}$ does not match the remaining string because its length equals to 9, not 10 ('374152368')

  • #14
    New to the CF scene
    Join Date
    Aug 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have no idea what you guys talking about? can someone educate me please.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •