There are many different ways in which numbers can be entered into forms. The exact validation that you need will depend on the design of your form and the kind of number that is being entered. This FAQ talks mostly about Clientside validation using JavaScript for which ChronoForms uses the MooTools FormCheck library.
Digit
This is the basic validation that will accept any string of the digits [0-9] with no spaces, commas or other characters allowed except for a + or - sign as a prefix.
The validation uses the Regular expression /^[-+]?[0-9]+$/
Valid:
1, 19, 109, 999999, +1, -9, etc.
Invalid:
0.101, a1, 1,000, >1, (1001), 999 999, etc.
Options
You can specify a range of numbers using the format validate[digit[21,65]] or a range with no upper limit i.e. just a minimum value, using -1 as the second paramter validate[digit[21,-1]]
To use this option set the validation in the Class box of the element, you can’t add this using the checkboxes on the validation tab.
Number
This validation accepts numbers any string of the digits [0-9] with a decimal point but no spaces, commas or other characters allowed except for a + or - sign as a prefix.
Note that this will not accept decimal point symbols other than . or currency symbols ot thousands separators of any kind.
The validation uses the Regular expression /^[-+]?\d*\.?\d+$/
Valid:
1, 0.101, 19, 109, 999999, +1, -9, etc.
Invalid:
a1, 1,000, >1, (1001), 999 999, $23 £99.99, 12 999,00, etc.
Custom validation
You can add a custom validation to accept other number formats if they are needed. Here is an example, the code needs to be in a Load JS action in your form:
function checkAmount(el){ if (!el.value.test(/^(?:^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$)$/i)) { el.errors.push("Please enter a valid amount"); return false; } else { return true; } };
The regex is from Regex Buddy and should validate "Number: Currency amount US & EU (cents optional). Can use US-style 123,456.78 notation and European-style 123.456,78 notation. Optional thousands separators; optional two-digit fraction"
Valid:
1, 0.101, 1,000, 19, 109, 999999, +1, -9, 999 999, 12 999,00, etc.
Invalid:
a1, >1, (1001), $23, £99.99, 100.999, etc.
Clean up
If you accept a range of different number formats as inputs you may need to clean the results up to give you a standard value to use in calculations or to save in the database. Here is an example of a PHP code snippet that can be adapted and used in a Custom Code action in the On Submit event to do this.
<?php $amount = $form->data[$amount_name]; $amount = preg_replace('#[^0-9,.]+#', '', $amount); $dec = substr($amount, -3, 1); if ( $dec == ',' || $dec == '.' ) { $dec = 2; } else { $dec = 0; } $amount = preg_replace('#[^0-9]+#', '', $amount); if ( $dec ) { $amount = $amount/100; } if ( !$amount || $amount <= 0 ) { $form->validation_errors['amount'] = 'Invalid number'; $invalid_data = true; } $amount = number_format($amount, $dec, '.', ''); $form->data['clean_amount'] = $amount; ?>