When validating prices with CI I use the following rule;
$this->form_validation->set_rules('price','lang:price','required|numeric|greater_than[0.99]');
Is there any way to allow commas in this rule line? Or do i have to create a callback?
Codeigniter 3 offers a regex validation. Form Validation
Using the regex given by Ben... No callback is required.
$this->form_validation->set_rules('price','lang:price','required|numeric|greater_than[0.99]|regex_match[/^[0-9,]+$/]');
From using the form validation library, I've never seen anything that would allow you to do that without a callback.
This would be the callback though:
function numeric_wcomma ($str)
{
return preg_match('/^[0-9,]+$/', $str);
}
with a rule of
$this->form_validation->set_rules('input', 'Input', 'callback_numeric_wcomma');
My answer is:
$this->form_validation->set_rules('price','lang:price','regex_match[/^(?!0*[.,]?0+$)\d*[.,]?\d+$/m]');
or do it in your codeigniter view. Add script:
<script>
function replace(element) {
// set temp value
var tmp = element.value;
// replace everything that's not a number or comma or decimal
tmp = tmp.replace(/[^0-9,.,-]/g, "");
// replace commas with decimal
tmp = tmp.replace(/,/, ".");
// set element value to new value
element.value = tmp;
}
</script>
and then use input like this:
<input id="something" onkeyup="replace(this)" type="text">