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 11 of 11
  1. #1
    New Coder
    Join Date
    May 2005
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Regex Help... Again :(

    Lo guys, I'm stuck with my regular expressions again.

    PHP Code:
    if (!eregi("^[[0-10]]*$"$_POST['rating'])){
    //display error

    The $_POST['rating'] needs to contain a value between 0 and 10, that's all. Can anyone tell me why the above code doesn't work if I put in 10, but works fine for all numbers below it.

    Thanks in anticipation.

  • #2
    New Coder
    Join Date
    May 2005
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    actually, it doesn't work at all. It works if I put 0-9 in place of 0-10

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It won't work because 0-9 means 1 character, any number. [0-10] means 1 character but 10 is actually 2 characters.

    You would need to use this I believe:

    PHP Code:
    if (!eregi("^[[0-9]{1,2}]*$"$_POST['rating'])){
    //display error


  • #4
    New Coder
    Join Date
    May 2005
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by SeeIT Solutions View Post
    It won't work because 0-9 means 1 character, any number. [0-10] means 1 character but 10 is actually 2 characters.

    You would need to use this I believe:

    PHP Code:
    if (!eregi("^[[0-9]{1,2}]*$"$_POST['rating'])){
    //display error

    Thanks for the help, but won't that allow any number up to 99?

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    True, one way to do it would be change the if statement to be either 0-9 or 10.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    It shouldn't work at all.
    PHP Code:
    $rating abs((int) $_POST['rating']);
    if(
    $rating 10)
    {
      
    // Display error;

    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    New Coder
    Join Date
    May 2005
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    thanks marek_mar.. i really wanted to do all my validation with regex, just for consistency more than anything else. But I've had to deviate in other areas, so I guess one more won't hurt.

    Thanks for the help guys.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You should use regex only if it is really needed... it's slower in most cases.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    New Coder
    Join Date
    May 2005
    Posts
    94
    Thanks
    4
    Thanked 0 Times in 0 Posts
    oh ok, thanks for the advice, i will take it on board..

  • #10
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Try /\A(?:\d|10)\z/xms
    Last edited by ralph l mayo; 11-17-2006 at 02:31 AM.

  • #11
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,839
    Thanks
    21
    Thanked 157 Times in 148 Posts
    PHP Code:
    if (preg_match('/^(0|1|2|3|4|5|6|7|8|9|10)$/'$_POST['rating']) == 0)
    {
       
    //display error


    that seems like it should work....
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪


  •  

    Posting Permissions

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