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?
There is no builtin date validation in Codeigniter
form_validation
Library, but you can use its callback to call a function and validate the date using PHP's own capabilities.With DateTime you can make the shortest date&time validator for all formats.
function was copied from this answer or php.net
you can do it with
regex
Using Regex is probably the best way. I use this Regex that I found here for the format YYYY-MM-DD: http://www.regular-expressions.info/dates.html
The actual Regex is:
It has a nice explanation on how each section works so that you can modify it for different date formats.
Then you can use the code suggested by @Abin: (make sure you enclose it in some kind of delimiter)