I'm using codeigniter with PHP
.
I'm using following form,
<?php
echo form_open('/register/create_new', $form_params);
?>
DOB: <input type="text" id="dob" name="reg[dob]">
<input type="submit" value="Create Account" />
</form>
here, #dob
is in dd-mm-yyyy
format.
my validation code is,
array(
'field' => 'reg[dob]',
'label' => 'DOB',
'rules' => 'required'
)
How can i set the rules
for correct date validation?
I know this is old, but I just encountered the same issue. I like the answer Winks provided, but found that the code did not work. I modified it to this:
I like the answer tmsimont provided, but found that the code did not work if non numeric values enterd. Modified code is given below:
I have a pretty clean solution for this. You can extend codeigniters form validation library.
Do this by putting a MY_Form_validation.php file in your application/libraries folder. This file should look like this:
To add the error message, you go to your application/language/ folder and open the form_validation_lang.php file. Add an entry to the $lang array at the end of the file, like so:
Note that the key here must be the same as the function name (valid_date).
Then in your controller you use this as any other form validation function like for example 'required'
You can take use of CodeIgniters callback functions by creating a callback_date_valid() function that check if the date is valid.
And to check if it is valid, you could use PHP's checkdate function
The actual Regex for Codeigniter Date validation :
I am using same expression for format dd-mm-yyyy and dd/mm/yyyy
No Warnings and errors :-)
I've written this custom validator for ci3 - validates the d/m/Y H:i format - you can easily change that.