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

Validation handler for the discrete version of the phone number.

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().


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


function form_example_phonenumber_discrete_validate($element, &$form_state) {
  if (isset($element['#value']['areacode'])) {
    if (0 == preg_match('/^\d{3}$/', $element['#value']['areacode'])) {
      form_error($element['areacode'], t('The area code is invalid.'));
  if (isset($element['#value']['prefix'])) {
    if (0 == preg_match('/^\d{3}$/', $element['#value']['prefix'])) {
      form_error($element['prefix'], t('The prefix is invalid.'));
  if (isset($element['#value']['extension'])) {
    if (0 == preg_match('/^\d{4}$/', $element['#value']['extension'])) {
      form_error($element['extension'], t('The extension is invalid.'));
  return $element;