Monday, November 30, 2009

Re: validate as hour

This regular Expression will force a number between 0-23

Raw Reg ex:
^1?[0-9]{1}$|^2[0-3]{1}$

I'm remembering for php some of these char in the regex provided need
to be escaped.
Formated for PHP:
/^1?[0-9]{1}$|^2[0-3]{1}$/

And here is a good on line real time tester for regular expressions:
http://www.regextester.com/

To break down the regular expression
^ - Start of line
1? - means the number '1' for 0 or 1 occurrences. This is used for the
first char of 10-19
[0-9]{1} - means digits 0,1,2,3,4,5,6,7,8,9 for {1} exactly one
character this covers 0-9 and the second char of 10-19
$ - End of line
| - or condition Match the previous set (0-19) or the following set
(20-23)
^ - Start of line
2 - the number 2 for the first char in 20-23
[0-3]{1} - means digits 0,1,2,3 for {1} exactly one character. This
covers the 0-3 or the second char when the first char is 2
$ - End of line


var $validate = array(
'hour' => array(
'required' => true,
'rule' => '/^1?[0-9]{1}$|^2[0-3]{1}$/',
'message' => 'Only numbers between 0-23'
)
);


On Nov 26, 11:41 am, "ge...@dinnerbooking.com"
<ge...@dinnerbooking.com> wrote:
> I have an field which is an hour:
>
> "hour" => array(
>         "required" => true,
>         "allowEmpty" => false,
>         "rule" => "/^\d{1,2}+/",
> ),
>
> but how do I also specify that the number MUST be between 0 and 23 ?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: