function form_validate_pattern

#element_validate callback for #pattern form element property.

Parameters

$element: An associative array containing the properties and children of the generic form element.

$form_state: The $form_state array for the form this element belongs to.

See also

form_process_pattern()

Related topics

1 string reference to 'form_validate_pattern'
form_process_pattern in drupal/core/includes/form.inc
#process callback for #pattern form element property.

File

drupal/core/includes/form.inc, line 3331
Functions for form and batch generation and processing.

Code

function form_validate_pattern($element, &$form_state) {
  if ($element['#value'] !== '') {

    // The pattern must match the entire string and should have the same
    // behavior as the RegExp object in ECMA 262.
    // - Use bracket-style delimiters to avoid introducing a special delimiter
    //   character like '/' that would have to be escaped.
    // - Put in brackets so that the pattern can't interfere with what's
    //   prepended and appended.
    $pattern = '{^(?:' . $element['#pattern'] . ')$}';
    if (!preg_match($pattern, $element['#value'])) {
      form_error($element, t('%name field is not in the right format.', array(
        '%name' => $element['#title'],
      )));
    }
  }
}