sdot257
sdot257

Reputation: 10366

With Codeigniter, how can I accept euro currency in form validation?

I'm validating my field against "decimal" but I would like to support the EURO. How can I validate the field seeing that euro's can have a decimal point or a comma? In my DB, I'm going to convert everything to an INT and multiply by 100.

Upvotes: 0

Views: 1240

Answers (1)

Ben Swinburne
Ben Swinburne

Reputation: 26467

You need to create a file in /application/libraries/ called MY_Form_validation.php

In this file you can override the decimal method used to validate decimals. Your file should look like the following

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation {
    /**
     * Decimal number
     *
     * @access  public
     * @param   string
     * @return  bool
     */
    function decimal($str)
    {
        return (bool) preg_match('/^[\-+]?[0-9]+[\.,][0-9]+$/', $str);
    }
}

// END Form Validation Extension Class

/* End of file MY_Form_validation.php */
/* Location: ./application/libraries/MY_Form_validation.php */

The difference between this method and the original is the [\.,] in the middle. In the original file, it's just \. which just allows a period .

If you don't want to change the way the decimal method works, you could rename it to euro for example and then just add euro as a validation rule.

Upvotes: 3

Related Questions