Blackout for SOPA and PIPA.
Click to learn more
Posterous theme by Cory Watilo

Validators - 2 - Age validation

Continuing the post Validators - the beginning
Media_https3amazonaws_thjgo
This post is about age validation. The implementation below let's you easily define a date that will be used as a reference date. Values same or older tan this date will produce the validation to pass, other will not. [cc lines="-1" lang="php" escaped="true"] This implementation assumes that the default validation only * validates for dates 13 years or older. * * @param string $reference The date to refer to. */ public function __construct($reference="-13 years") { $this->_referenceAge = strtotime($reference); } /** * Validates that the given date is earlier or equal than the * reference age * * @param string $date An strtotime capable string * * @return bool true if valid, false otherwise */ public function validate($date) { $val = strtotime($date); return $val_referenceAge; } } ?> [/cc]

unit test

[cc lines="-1" lang="php" escaped="true" theme="blackboard" width="510"] object = new AgeValidator(); } else { $this->object = new AgeValidator($refDate); } $actual = $this->object->validate($value); $this->assertEquals($expected, $actual); } } ?> [/cc]