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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Quickform Regex expressions

    I am using Quickform to validate data and then insert it into a database. I have everything running smoothly except the following.

    I am creating a rule that says the date must be entered in YYYY-MM-DD format, however it's not accepting it when I enter it that way.
    PHP Code:
    $form->addRule('order_date''Please enter date as yyyy-mm-dd''regex''(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])'); 
    Last edited by Tiney83; 05-11-2009 at 06:01 PM.

  • #2
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Id have to see the class really to see what thats doing. the the regex doesn't look quite right. There's no delimiters for starters. I can test the regex for you but again would have to see the class structure for the object $form
    Last edited by timgolding; 05-11-2009 at 10:13 PM.
    You can not say you know how to do something, until you can teach it to someone else.

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    The regex works other than not having delimiters. This would work using preg
    #(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])#

    e.g

    PHP Code:
    preg_match("#(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])#","2009 12 20"$matches);
    print_r($matches); 
    outputs

    Code:
    Array
    (
        [0] => 2009 12 20
        [1] => 20
        [2] => 12
        [3] => 20
    )
    I tested this for a few other example and it worked fine. I'm guessing either a) you forgot to put on the delimiters or b) your class does that for you which would be kinda stupid. For instance if it did that for you then how would it know what char to use if it just padded a / on then your regex would fail because it has a / in it. Anyway give that a try!

    PHP Code:
    $form->addRule('order_date''Please enter date as yyyy-mm-dd''regex''#(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])#'); 
    If it doesn't work show me the class or specifically the class function addRule.

    Personally i prefer select boxes for date selection or even a nice calendar chooser.
    Last edited by timgolding; 05-11-2009 at 10:48 PM. Reason: You can't include the delimiter in a character class
    You can not say you know how to do something, until you can teach it to someone else.

  • Users who have thanked timgolding for this post:

    Tiney83 (05-12-2009)

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    8
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks very much, it worked.


  •  

    Posting Permissions

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