Server side validation for birth year

cyclotan 24 Aug, 2013
I wanted to check a birth year field which must a number between 1900 and 2099 by using server side validation.
I adapted your FAQ code as follow:

<?php
$data =& $form->data['annee_naissance'];
$regex = '^(19|20)\d{2}$';
if ( isset($data) && $data ) {
  if ( preg_match($regex, $data) == 0 ) {
    $form->validation_errors['annee_naissance'] = "Invalid data";
    return false;
  }
}
?>


but get the following error :

Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /mysite /custom_serverside_validation.php(18) : eval()'d code on line 5

What is wrong ?
ps: sorry, I am completly new in php coding
GreyHead 24 Aug, 2013
Hi cyclotan,

That looks complicated :-(

<?php
if ( isset($form->data['annee_naissance']) && $form->data['annee_naissance'] ) {
  if ( $form->data['annee_naissance'] < 1900 || $form->data['annee_naissance'] > 2099 ) {
    $form->validation_errors['annee_naissance'] = "Invalid data";
    return false;
  }
}
?>

Bob
cyclotan 25 Aug, 2013
Yes, it's largely better like that and it works.

Thanks a lot
This topic is locked and no more replies can be posted.