Hi mergui,
I'm not sure if this helps but here's a code snippet to clean up a number and convert it to a 99999.99 format for saving in the database. You can use it in a Custom Code action or a Custom Serverside Validation action in the On Submit event.
<?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'] = JText::_( 'CF_AA_GH_ERROR_AMOUNT' );
$invalid_data = true;
}
$amount = number_format($amount, $dec, '.', '');
?>
Client-side validation is trickier, I think that the answer is to add a custom amount validator using code like this in a Load JS action:
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"
To validate an amount input add
class="validate['%checkAmount'] to the input.
Bob