example_element_phonenumber_validate

Versions
6 – 7
example_element_phonenumber_validate($form, &$form_state)

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

developer/examples/example_element.module, line 100

<?php
function example_element_phonenumber_validate($form, &$form_state) {
  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;

}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.