Date Validation in Jquery


In this article, I will explain how to validate date field in JQuery. simple way on how to use jQuery and javascript to validate a certain date. if you are working with Start Date and End date than End Date must be Greater than Start Date.How to validate by just adding class. I'm using the jQuery validator plugin with a custom validation method.

In this article, I will explain how to validate date field in JQuery. simple way on how to use jQuery and javascript to validate a certain date.
if you are working with Start Date and End date than End Date must be Greater than Start Date.How to validate by just adding class.

I'm using the jQuery validator plugin with a custom validation method added using the addMethod(name, function, message) method.

Add a custom validation method. It must consist of a name , a javascript based function and a default string message.


E.g.:End Date should be greater than Start Date



<html>
<head>
<title>Date Validation</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js"></script>
<script type="text/javascript" src="date.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('form').validate();
DateValidation();
});

function DateValidation() {
$.validator.addMethod(
"enddate",
function (value, element) {
var sdatevalue = $('.startdate').val();

return Date.parse(sdatevalue) < Date.parse(value);

},
"End Date should be greater than Start Date."
);

$('.enddate').validate({
name: "enddate"
});
}
</script>
</head>
<body>
<form method="post">
<label>
Start Date:
<input type="text" class="startdate" value="17/12/2011"/>

EndDate:
<input type="text" class="enddate" />

</label>
<button>Validate</button>
</form>
</body>
</html>




I hope this will help you.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: