#1
  1. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141

    Check for 3 optional comma-separated values


    I have a html form with 3 checkboxes: Alpha, Beta, Gamma.

    The values are stored in a comma-separated string, like "Alpha,Beta,Gamma". Each one is optional and they don't have to appear in any particular order (i.e. "Gamma,Beta" is valid).

    What's the simplest way to validate this with a regular expression?

    I can't think of a way to do this without allowing duplicates:
    ^(sportsbook|casino|poker)(,(sportsbook|casino|poker)){0,2}$
    Last edited by DrWorm; September 2nd, 2009 at 11:36 PM.
    Ooh, they have the Internet on computers now!
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    If they're in checkboxes, they come in through the POST data in their own entries, why not just check the entries separately?

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141
    Originally Posted by ManiacDan
    If they're in checkboxes, they come in through the POST data in their own entries, why not just check the entries separately?

    -Dan
    It's not the way this system works.
    Ooh, they have the Internet on computers now!
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Probably easiest to split the string on the comma and make sure each entry is within the acceptable values. Regexp could be used for this, but regular string parsing would be faster.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141
    Originally Posted by ManiacDan
    Probably easiest to split the string on the comma and make sure each entry is within the acceptable values. Regexp could be used for this, but regular string parsing would be faster.
    Ideally, yes, but that's not an option for me.
    Ooh, they have the Internet on computers now!
  10. #6
  11. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Eh? Well, why isn't it an option?
    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
    User 165270
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2005
    Posts
    497
    Rep Power
    937
    Originally Posted by DrWorm
    Ideally, yes, but that's not an option for me.
    As already mentioned by others, regex isn't well suited for this task, but it IS possible:

    ^(Alpha(?!.*?Alpha)|Beta(?!.*?Beta)|Gamma(?!.*?Gamma))(,(Alpha(?!.*?Alpha)|Beta(?!.*?Beta)|Gamma(?!. *?Gamma))){0,2}$

    Comments on this post

    • DrWorm agrees : Thank you. This gives me a good appreciation for the complexity of the problem. And now I don't feel like I was missing something obvious :)
    • ManiacDan agrees
    Last edited by prometheuzz; September 5th, 2009 at 02:05 PM.
  14. #8
  15. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Was this a homework assignment or something? Doing it in regexp is difficult and very slow.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  16. #9
  17. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141
    Originally Posted by ManiacDan
    Was this a homework assignment or something?
    No. Working with a custom API which describes and validates a form.
    Ooh, they have the Internet on computers now!
  18. #10
  19. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,127
    Rep Power
    4304
    Oh. So you can only pass it a custom regex? There's no option to pass it a custom function?
    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).
  20. #11
  21. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2002
    Location
    Queensland, Australia
    Posts
    827
    Rep Power
    141
    Originally Posted by Kravvitz
    Oh. So you can only pass it a custom regex? There's no option to pass it a custom function?
    Pretty much, yes.
    Ooh, they have the Internet on computers now!
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Ouch. Maybe you should get a new validator, something like that should really accept a callback.

    Or, in PHP6, a lambda function. I can't wait.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo