Run a boolean test function on form submit
You should do this in validation handler. In hook_form_alter (or in your form definition function), add $form['#validation'][] = 'mymodule_form_validate';. Then, add this code:

function mymodule_form_validate($form, &amp;$form_state) {
  if (!mymodule_dates_valid($form_state)) { // Return boolean here.
    form_set_error('date_field', t('Invalid dates'));
  }
}