1 form_example_elements.inc form_example_phonenumber_combined_validate($element, &$form_state)

Phone number validation function for the combined phonenumber.

Uses regular expressions to check that:

  • the area code is a three digit number
  • the prefix is numeric 3-digit number
  • the extension is a numeric 4-digit number

Any problems are shown on the form element using form_error().

The combined value is then updated in the element.

File

modules/examples/form_example/form_example_elements.inc, line 348
This is an example demonstrating how a module can define custom form and render elements.

Code

function form_example_phonenumber_combined_validate($element, &$form_state) {
  $lengths = array(
    'areacode' => 3,
    'prefix' => 3,
    'extension' => 4,
  );
  foreach ($lengths as $member => $length) {
    $regex = '/^\d{' . $length . '}$/';
    if (!empty($element['#value'][$member]) && 0 == preg_match($regex, $element['#value'][$member])) {
      form_error($element[$member], t('@member is invalid', array('@member' => $member)));
    }
  }

  // Consolidate into the three parts into one combined value.
  $value = $element['areacode']['#value'] . $element['prefix']['#value'] . $element['extension']['#value'];
  form_set_value($element, $value, $form_state);
  return $element;
}