You are here

function webform_number_format_match in Webform 7.4

Same name and namespace in other branches
  1. 6.3 components/number.inc \webform_number_format_match()
  2. 7.3 components/number.inc \webform_number_format_match()

Validates if a provided number string matches an expected format.

This function allows the thousands separator to be optional, but decimal points must be in the right location.

A valid number is: 1. optional minus sign. 2. optional space. 3. the rest of the string can't be just a decimal or blank. 4. optional integer portion, with thousands separators. 5. optional decimal portion, starting is a decimal separator. Don't use preg_quote because a space is a valid thousands separator and needs quoting for the 'x' option to preg_match.

Based on http://stackoverflow.com/questions/5917082/regular-expression-to-match-n....

3 calls to webform_number_format_match()
_webform_render_number in components/number.inc
Implements _webform_render_component().
_webform_submit_number in components/number.inc
Implements _webform_submit_component().
_webform_validate_number in components/number.inc
A Drupal Form API Validation function.

File

components/number.inc, line 829
Webform module number component.

Code

function webform_number_format_match($value, $point, $separator) {
  $thousands = $separator ? "\\{$separator}?" : '';
  $decimal = "\\{$point}";
  return preg_match("/\n                    ^               # Start of string\n                    -?              # Optional minus sign\n                    \\ ?             # Optional space\n                    (?!\\.?\$)        # Assert looking ahead, not just a decimal or nothing\n                      (?:           # Interger portion (non-grouping)\n                        \\d{1,3}     #   1 to 3 digits\n                        (?:         #   Thousands group(s)\n                        {$thousands}  #     Optional thousands separator\n                        \\d{2,3}     #     2 or 3 digits. Some countries use groups of 2 sometimes\n                      )*            #   0 or more of these thousands groups\n                    )?              # End of optional integer portion\n                    (?:             # Decimal portion (non-grouping)\n                      {$decimal}      #   Decimal point\n                      \\d*           #   0 or more digits\n                    )?              # End of optional decimal portion\n                    \$\n                    /x", $value);
}