You are here

function value_is_number in Value is 7

Checks for number (floating point number).

File

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

Code

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

    // Not even close.
    if (!_value_is_number($value)) {
      return form_error($element, t('Only numbers and the decimal separator are allowed in %field.', array(
        '%field' => $element['#title'],
      )));
    }

    // Too low.
    if (isset($element['#min']) && $value < $element['#min']) {
      return form_error($element, t('%name: the value may be no less than %min.', array(
        '%name' => $element['#title'],
        '%min' => $element['#min'],
      )));
    }

    // Too high.
    if (isset($element['#max']) && $value > $element['#max']) {
      return form_error($element, t('%name: the value may be no greater than %max.', array(
        '%name' => $element['#title'],
        '%max' => $element['#max'],
      )));
    }

    // Cast input value to a Float.
    form_set_value($element, (double) $value, $form_state);
  }
}