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

    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Regex validation


    trying to validate for numeric or exactly 'n/a'.

    tried [0-9]|^(n\/a)$
    but it passes 111 n/a. any suggestions. should
    i pick a different profession?
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,303
    Rep Power
    9400
    Not sure if want to answer.

    The ^$s need to be around the whole thing, otherwise it's just a normal "contains a"-type match.
    Code:
    #^(\d+|n/a)$#
  4. #3
  5. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    Welcome to DevShed Forums, yoqi.

    Since you're asking if you should give up at the same time you're asking the first question, then perhaps you should find something else to do since you don't seem very committed.

    More seriously, programming, including regular expressions, takes time to learn.

    Your expression isn't working because you're saying that a single digit appearing anywhere within the string is valid when you didn't intend to. You want the "^" at the very beginning and the "$" at the very end. You also want to use the "+" to allow one or more numerals. I suggest you use "\d", which is the shorthand for "[0-9]". The "?:" immediately inside the parentheses means that the group won't be a capturing group.
    Code:
    ^(?:\d+|n\/a)$
    Now if you want to allow decimals and negative numbers as well, you can use this:
    Code:
    ^(?:\-?\d*(?:\.\d+)?|n\/a)$
    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).
  6. #4
  7. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    94
    Hi yoqi,

    requinix and Kravvitz have already answered beautifully.

    I'd just like to explain this a bit more:
    Code:
    it passes 111 n/a
    What happens when the regex "[0-9]|^(n\/a)$" tries to match the string "111 n/a"?

    It matches the first 1. The "match" result does not mean that the match was the entire string. The engine reports "match" because it found a match. That match was the first 1.

    The regex starts with [0-9]: match one digit. You feed it "111 n/a". Okay, the engine can match the initial 1. Then you say "OR..." (the alternation operator). So the engine is done.

    As the others have explained, if you want to make sure the regex matches one thing and only that thing (e.g. the string "thing"), you need to say something like "^thing$". This means "match the beginning of the string, then the letter t, then the letter h, then the letter i, then the letter n, then the letter g, then the end of the string. So that regex would not match "things", because after the g the engine is not able to match the end of the string (specified by the $ anchor).

    Wishing you all a fun day

    Comments on this post

    • Kravvitz agrees
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    picking different profession


    that was tongue in cheek. i'm actually a professional. i don't work with regex much and when i do, i can't seem to concentrate. it's like trying to read music at my age.

    anywho, it works in php when i coded:
    $rule = '#^(?:\d+|n\/a)$#';
    if(isset($_POST['button'])){
    $in = $_POST['in'];
    if (preg_match($rule,$in)) {
    echo 'good';
    }else{
    echo 'bad';
    }
    }

    however, i need to place the regex in a column in a database to allow changes, but when i placed the #^(?:\d+|n\/a)$#
    in a column in a database, the regex doesn't match. not sure why???
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    You probably need to use stripslashes: http://us2.php.net/manual/en/function.stripslashes.php

    I'm finding that reading threads in this forum is a good way to learn more about regex.

    P.S. It would be helpful if you put your code between [code][/code] tags or [php][/php] tags in the future.
    Last edited by Kravvitz; February 14th, 2012 at 07:37 PM.
    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).
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    regex in database


    doh! i had to escape the backslashes when inserting into the db.

    #^(?:\\d+|n\\/a)$#

    thanks for all your help.

IMN logo majestic logo threadwatch logo seochat tools logo