How to validate minimum age

SPABO 14 Nov, 2012
On the server side validation I would like to "check"the age of the subscriber, his/het age should be 14 years as from 2012-01-01 (yyyy-mm-dd)

Tablename : J25_chronoforms_data_MP_2
Birthday : cb_geboren

I tried this

<?php
$geboren = JRequest::getString('cb_geboren', '', 'post');
$db =& JFactory::getDBO();
$query = "
SELECT COUNT(*)
FROM `J25_chronoforms_data_MP_2`,
WHERE `cb_lidcode` = '{$geboren}';
$db->setQuery($query);
if ($geboren > 1949-01-01){
$form->validation_errors[''] = 
"Too old";
return false;}
?>

However, this gives errors..:(

GreyHead 15 Nov, 2012
Hi SPABO,

It looks as though the quote to end the query and the line to execute the query $xxx = $db->getResult(); are missing - but I can't see that the query does anything at all??

You also can't usefully compare dates as strings (and the quotes are missing round the 1949 date too).
<?php
$geboren = $form->data['cb_geboren'];
/*
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(*)
    FROM `#__chronoforms_data_MP_2`,
    WHERE `cb_lidcode` = '{$geboren}';
"; // add this line
$db->setQuery($query);
$db->getResult(); // add this line
*/
if ( strtotime($geboren) > strtotime('1949-01-01') ) {
  $form->validation_errors['cb_geboren'] = "Too old";
  return false;
}
?>

Bob
SPABO 15 Nov, 2012
Dear Bob,
I'm a fan of Chronforms, however I'm lacking PHP skills.
I now have this code in (to define TOO YOUNG)
Tx again !!
<?php
$geboren = $form->data['cb_geboren'];
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(*)
    FROM `J25_chronoforms_data_CK MATCHPLAY_2`,
    WHERE `cb_lidcode` = '{$geboren}';
"; 
$db->setQuery($query);
$db->getResult();
if ( strtotime('1949-01-01')< strtotime($geboren) ) {
  $form->validation_errors['cb_geboren'] = "Sorry, maar je bent te jong om aan deze wedstrijd deel te nemen.";
  return false;
}
?>
GreyHead 18 Nov, 2012
Hi SPABO,

What exactly is the problem?

The code looks OK - except that the DB query still does nothing.

Bob
SPABO 18 Nov, 2012
Hi Bob,
What I tried to say the code as below does work nicely

But now I'm puzzled..

except that the DB query still does nothing


Pls advise

Rgds
Kees
GreyHead 19 Nov, 2012
Hi Kees,

Not much to say, the output from the query isn't assigned to a variable and isn't used anywhere. This code would do the same:
<?php
if ( strtotime('1949-01-01') < strtotime($form->data['cb_geboren']) ) {
  $form->validation_errors['cb_geboren'] = "Sorry, maar je bent te jong om aan deze wedstrijd deel te nemen.";
  return false;
}
?>

Bob
SPABO 19 Nov, 2012
Hi Bob
The last code works fine as well!
tx again
Rgds
Kees
This topic is locked and no more replies can be posted.