Welcome Guest, Not a member yet? Register   Sign In
Custom Form Validation function don't work
#1

[eluser]sohei[/eluser]
Hi all,
I have a problem with my form_validation. It always return FALSE.

I have to validate a date input, so that's what i've done:

My rule in my 'form' function:
Code:
$this->form_validation->set_rules('input_Date', 'Date', 'callback_date_check');

And my custom validation function:
Code:
function date_check($str) {
            if (preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/',$str)){
                return TRUE;
            } else {
                $this->form_validation->set_message('date_check', 'Incorrect date format');
                return FALSE;
            }
        }

It looks like he does not recognize $str. If that..What should I put instead?

Thanks!
sohei
#2

[eluser]smilie[/eluser]
Your function should look like:

Code:
function date_check() {
            if (preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/',$this->input->post('input_date')){
                return TRUE;
            } else {
                $this->form_validation->set_message('date_check', 'Incorrect date format');
                return FALSE;
            }
        }

$str is not defined anywhere - thus you always get FALSE.
Simply use: this->input->post('input_field_name') and it should work correctly.

Cheers,
Smilie
#3

[eluser]farhad zand[/eluser]
Code:
function date_check() {
            if (preg_match('/^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$/',$this->input->post('input_date')){
                return TRUE;
            } else {
                $this->form_validation->set_message('date_check', 'Incorrect date format');
                return FALSE;
            }
        }

Go gon take away
#4

[eluser]sohei[/eluser]
Arf,
It works now, thanks.

PS:I found it strange too, but in the userguide example, $str is not defined either. So I thought it was a default attribute.
(http://ellislab.com/codeigniter/user-gui...l#tutorial)




Theme © iAndrew 2016 - Forum software by © MyBB