View Full Version : Quickform Regex expressions

05-11-2009, 05:45 PM
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.

$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])');

05-11-2009, 09:55 PM
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

05-11-2009, 10:12 PM
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])#


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


[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!

$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.

05-12-2009, 05:36 AM
Thanks very much, it worked. :D