You are here

function form_validate_decimal in Form Builder 6

Same name and namespace in other branches
  1. 7.2 form_builder.module \form_validate_decimal()
  2. 7 form_builder.module \form_validate_decimal()

Generic validation function to check that an element has a decimal value.

File

./form_builder.module, line 378
form_builder.module Generic form building framework and user interface.

Code

function form_validate_decimal(&$element, &$form_state) {
  $value = $element['#value'];
  if (empty($value)) {
    return;
  }

  // Remove thousands separators from numbers.
  $locale_info = localeconv();
  $separator = empty($locale_info['thousands_sep']) ? ',' : $locale_info['thousands_sep'];
  $new_value = str_replace(array(
    ' ',
    $separator,
  ), '', $value);
  $float_value = $new_value * 1.0;
  $new_value = (string) $float_value;
  if (!is_float($float_value) || $float_value === 0.0) {
    form_error($element, t('The %title field value must be a decimal.', array(
      '%title' => $element['#title'],
    )));
  }
  elseif ($new_value !== $value) {
    form_set_value($element, $new_value, $form_state);
  }
}