Custom Date Validation ‘DD/MM/YYYY’ using jquery.validation.js

Javascript custom date validation code using jquery validation plugin and codeigniter php framework to validate and accept the date in dd/mm/yyyy format.

Required things :

1) A latest jquery js file i.e. jQuery.js ( http://jquery.com/download/)
2) jquery.validation.js (download it from jqueryvalidation.org )

HTML+PHP code :

This tutorial contain a PHP-CodeIgniter HTML rendering code, so if you want this tutorial for HTML only then replace codeigniter form code with your html form code and use this solution.
You can do other validations like required, email, date, dateISO, numeric etc using this validation plugin. The complete documentation is available on this link (jquery.validation Documentation)

<?php
$this->load->helper('form');
$attributes = array('class'=>'reg', 'id'=>'regform' , 'method' => 'post');
echo form_open("user/registerQuickUser", $attributes);
?>
<label for="date_of_birth" class="inline">Date of Birth</label>
<?php
echo form_input('date_of_birth', set_value('date_of_birth'), 'class="" id="date_of_birth" placeholder="DD/MM/YYYY"');
?>
<?php
$submit_options = array('name' => 'submit' ,'id' => 'mysubmit','value' => 'Register',
'class' => 'button radius small');
echo form_submit($submit_options);
?>

jQuery Code Рcustom date validation :

Here is the jquery code for this. add this code in your page in script tag (<script></script>)

$(document).ready(function(){
  var form = $("#regform");
  var validator = form.validate({
    rules: {date_of_birth:{required : true, checkDateFormat: true}},
    messages:{date_of_birth:{checkDateFormat : "Please enter correct date."}}
  });
});

jQuery.validator.addMethod('checkDateFormat', function(value, element){
var stringPattern = /^(?:(?:31(/|-|.)(?:0?[13578]|1[02]))1|(?:(?:29|30)(/|-|.)(?:0?[1,3-9]|1[0-2])2))(?:(?:1[6-9]|[2-9]d)?d{2})$|^(?:29(/|-|.)0?23(?:(?:(?:1[6-9]|[2-9]d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1d|2[0-8])(/|-|.)(?:(?:0?[1-9])|(?:1[0-2]))4(?:(?:1[6-9]|[2-9]d)?d{2})$/gm;

  if(stringPattern.test(value)){ return true; }
  else { return false; }
},"Please enter correct date.");