#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Accept 8, 12 or 13 digits


    Hello,

    I'd like to check whether the user typed in exactly a 8, 12 or 13 digit number. Other combination are not allowed.
    I couldn't manage to create a properly working regex expression.
    Any help, please?

    Thanks in advance.
    Silver.
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,122
    Rep Power
    4258
    Well what have you tried?

    These might tell you the part that you don't know:
    http://www.regular-expressions.info/repeat.html
    http://php.net/manual/en/regexp.refe...repetition.php
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by Kravvitz
    Well what have you tried?
    Well, it was easy when I only accepted 12 or 13 digits:
    \d{12,13}

    but I couldn't figure out how to accept 8 digits as well.
    \d{8,13} obviously didn't work because it verified 9,10 and 11 digits which are not allowed.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    There is no special expression for a varying number of occurences.

    Either you simply test for "8 digits or 12-13 digits". Or you express this as "8 digits optionally followed by 4-5 digits":

    Code:
    ^\d{8}(?:\d{4,5})?$
    Don't forget the anchors "^" and "$". Otherwise, the regex will also match substrings (depending on the programming language).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by Jacques1
    ...
    Thanks a lot, Jacques.
    I guess I'll have to learn how to use anchors...

    Cheers.
    Silver.

IMN logo majestic logo threadwatch logo seochat tools logo