You are here

function value_is_email in Value is 7

Checks for e-mail address(es).

1 call to value_is_email()
value_is_email_or_token in ./value_is.module
Checks for e-mail address(es) or token(s).

File

./value_is.module, line 214
All the element validators for #element_validate.

Code

function value_is_email($element, &$form_state, $form) {
  $value = $element['#value'];
  if ('' != $value) {
    $allow_tokens = !empty($element['#allow_tokens']);

    // Default is just 1 address.
    $max = isset($element['#max']) ? (int) $element['#max'] : 1;

    // The input will be validated as if it were an array.
    $addresses = explode(',', $value);

    // Too many.
    if ($max < count($addresses)) {
      $params = array(
        '%title' => $element['#title'],
        '@max' => $max,
      );
      $message = t('%title can contain max @max e-mail addresses.', $params);
      return form_error($element, $message);
    }

    // Validate per address.
    foreach ($addresses as $address) {
      if (!valid_email_address(trim($address)) && (!$allow_tokens || !preg_match('#:e?mail\\]#', $address))) {
        $params = array(
          '%title' => $element['#title'],
        );
        $message = 1 < $max ? t('%title must be a comma separated list of valid e-mail addresses.', $params) : t('%title must be a valid e-mail address', $params);
        return form_error($element, $message);
      }
    }
  }
}