example_element_validate

5 example_element.module example_element_validate($form)

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

1 string reference to 'example_element_validate'

File

developer/examples/example_element.module, line 106
This is an example demonstrating how a module can define custom form elements.

Code

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;

}
Login or register to post comments