example_element_validate

Definition

example_element_validate($form)
developer/examples/example_element.module, line 106

Description

Our element's validation function.

We check that:

  • the area code is a three digit number
  • the number is numeric, with an optional dash
Any problems are attached to the form element using form_error().

Code

<?php
function example_element_validate($form) {
  if (isset($form['#value']['areacode'])) {
    if (0 == preg_match('/^\d{3}$/', $form['#value']['areacode'])) {
      form_error($form['areacode'], t('The areacode is invalid.'));
    }
  }
  if (isset($form['#value']['number'])) {
    if (0 == preg_match('/^\d{3}-?\d{4}$/', $form['#value']['number'])) {
      form_error($form['number'], t('The number is invalid.'));
    }
  }
  return $form;
  
}
?>
 
 

Drupal is a registered trademark of Dries Buytaert.