Friday, March 14, 2014

Re: Custom MM:SS Time Validation with Regex

Hmmm thanks for that code you've given me something to think about.

Would I implement that in my model and the call it before my save in the controller?

On Tuesday, 11 March 2014 19:44:14 UTC+11, AD7six wrote:
If regular expressions aren't your thing - don't use regular expressions =)

e.g. write a validation function like so:

    function validateTime($check) {
        $string = current($check);
        list($mins, $seconds) = expode(':', $string);

        $validMins = is_numeric($mins);
        $validSeconds = is_numeric($seconds) && ($seconds < 60);

        return $validMins && $validSeconds;
    }

Alternatively since a regex is not cakephp specific - ask in an appropriate place (stack overflow) with appropriate context i.e. "how can I check that a string matches the format xx:yy where <it's time>?" Or - check for existing questions asking exactly that, like this one: http://stackoverflow.com/questions/3964972/validate-this-format-hhmm and modify to your needs

AD


On Tuesday, 11 March 2014 05:03:28 UTC+1, Justin Atack wrote:
Anybody?

On Friday, 7 March 2014 22:14:03 UTC+11, Justin Atack wrote:
Hi All,

I need to validate a field that will store a time instance MM:SS e.g. 04:32 would represent 4 minutes and 32 seconds.

I have created the code in my model to validate the time field but I really don't know much about regex. Here are a few that work ok...

/([0-9]\d|60):([0-9]\d|60)/

^([0-5]?[0-9]):([0-5]?[0-9])^

but neither causes an error if the time is 99:99. This is ok for the MM part as 99 minutes is valid but the SS part should only ever have a max of 59 (seconds).

I hope that makes sense!

This is the code in my model this far

'time' => array(
'custom' => array(
'rule' => array('custom', '/([0-9]\d|60):([0-9]\d|60)/'), //MM:SS
//'rule' => array('custom', '^([0-5]?[0-9]):([0-5]?[0-9])^'), //MM:SS
'message' => 'Time must be entered as MM:SS. e.g. 04:32 representing 4 minutes and 32 seconds.',
'allowEmpty' => true,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'maxLength' => array(
'rule' => array('maxLength', '5'),
'message' => 'Maximum length of time input is five characters. e.g. MM:SS',
//'allowEmpty' => true,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: