简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM