function isValidDate(startDate,endDate,datetocheck) {
var sd = Date.parse(startDate);
var ed = Date.parse(endDate);
var dc = Date.parse(datetocheck);
if((dc <= ed && dc >= sd)) {
return true;
}
return false;
}
http://www.kbdump.com/
Marked as answer by sunny43 on Nov 14, 2010 06:45 AM
$.validator.addMethod("CheckDOB", function (value, element) {
// checking whether the date entered is in correct format
var isValid = value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/);
if(isValid){
var minDate = Date.parse("01/01/1990");
var today = new Date();
var DOB = Date.parse(value);
if ((DOB >= today || DOB <= minDate)) {
isValid = false;
}
return isValid;
}
}, "NotValid");
Regards,
Naveen Jose ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
Marked as answer by sunny43 on Nov 14, 2010 06:58 AM
Sunny I will strongly recommend to use jQuery Datepicker. For user point of view it is the best option . I will never go and re-invent the wheel what you are trying to do . Always see from the user perspective. Also for jQuery datepicker for DOB purpose
if there are no years and months dropdowns it will be useless. Here is the complete sample:
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css"
type="text/css" media="all" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js"
type="text/javascript"></script>
<script type="text/javascript">
$(function () {
var minDate = new Date('1/1/1990');
var todaysDate = new Date();
var maxDate = new Date(todaysDate.getFullYear(),
todaysDate.getMonth(),
todaysDate.getDate() - 1);
var currentsYear = todaysDate.getFullYear();
var range = '1900:' + currentsYear
$('#txtDOB').datepicker({
minDate: minDate,
maxDate: maxDate,
changeMonth: true,
changeYear: true,
yearRange: range
});
});
</script>
</head>
<body>
<input type="text" id="txtDOB" />
</body>
</html>
user going to enter the date like 11202010 then I have do some coding to convert that date to 11/20/2010..
mask will automatically adds / after 2 numbers.....
I dont know either this feature is available in datepicker or not..I am very happy to use datepicker but i don't know either i will fit for my requirment or not..
sunny43
Member
126 Points
170 Posts
How check Date of Birth in Jquery Validation
Nov 14, 2010 01:50 AM|LINK
Anyone please tell me how to check the Date of birth in jquery validation or in Jquery....
I know how to check with the Regex but I want to do like real DOB checking ...
User should not enter today's or future date and must be greater than 1/1/1990...
Need urgent help..thank you....
naveenj
Contributor
6164 Points
1130 Posts
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 03:58 AM|LINK
Hi sunny43,
A better way will be using jQuery UI DatePicker on a asp:TextBox with Enabled="false"
Here is an example
http://jsfiddle.net/yetanothercoder/FAH4b/
Dependencies:
jQuery UI
jQuery UI CSS
Naveen Jose
ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
sunny43
Member
126 Points
170 Posts
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 04:58 AM|LINK
Oh..no...this is a good way but not in my requirement...
I am using Jquery validation plugin for validation...I found some regex examples but those will allow today's and future date...
I am very new to Jquery..I a have some idea but I don't how implement is....
Taking like
function(value, element){
var olddate=new date("1/1/1990);
var inputdate = new date(value)
var current=new currentdate...(don't know how get that)
checking for olddate<inputdate<current
}
It seems to be very stupid...but I don't know exactly how code in Jquery....
Anyway thanks for your reply...
Anirudh.Gupt...
Member
633 Points
171 Posts
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 05:28 AM|LINK
why dont you use datepicker like ?
var olddate = new date("1/1/1990); var currentdate = new Date(); $( ".selector" ).datepicker({ minDate: olddate, maxDate:currentdate });reference http://jqueryui.com/demos/datepicker/
but if you anyways want to validate try following
function isValidDate(startDate,endDate,datetocheck) { var sd = Date.parse(startDate); var ed = Date.parse(endDate); var dc = Date.parse(datetocheck); if((dc <= ed && dc >= sd)) { return true; } return false; }sunny43
Member
126 Points
170 Posts
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 06:44 AM|LINK
Here is my working code for adding a method to JQuery Validator...
$.validator.addMethod("CheckDOB", function (value, element) {
var minDate = Date.parse("01/01/1990");
var today=new Date();
var DOB = Date.parse(value);
if((DOB <= today && DOB >= minDate)) {
return true;
}
return false;
}, "NotValid");
Very nice and working fine...thanks for the help....
naveenj
Contributor
6164 Points
1130 Posts
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 06:53 AM|LINK
Hi sunny43,
Thats good.
I have done a simple alteration
$.validator.addMethod("CheckDOB", function (value, element) { // checking whether the date entered is in correct format var isValid = value.match(/^\d\d?\/\d\d?\/\d\d\d\d$/); if(isValid){ var minDate = Date.parse("01/01/1990"); var today = new Date(); var DOB = Date.parse(value); if ((DOB >= today || DOB <= minDate)) { isValid = false; } return isValid; } }, "NotValid");Naveen Jose
ASP.NET Freelancer, Consultant
Please remember to click Mark as Answer on the post that helps you
sunny43
Member
126 Points
170 Posts
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 07:00 AM|LINK
Yes Naveen..its more practical to check for the format...thanks for the change and I planning to have a mask for that...
forcing the user to enter in that format only....cool...
Thanks for your prompt replys....
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 07:04 AM|LINK
Sunny I will strongly recommend to use jQuery Datepicker. For user point of view it is the best option . I will never go and re-invent the wheel what you are trying to do . Always see from the user perspective. Also for jQuery datepicker for DOB purpose if there are no years and months dropdowns it will be useless. Here is the complete sample:
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css" type="text/css" media="all" /> <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.4.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/jquery-ui.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { var minDate = new Date('1/1/1990'); var todaysDate = new Date(); var maxDate = new Date(todaysDate.getFullYear(), todaysDate.getMonth(), todaysDate.getDate() - 1); var currentsYear = todaysDate.getFullYear(); var range = '1900:' + currentsYear $('#txtDOB').datepicker({ minDate: minDate, maxDate: maxDate, changeMonth: true, changeYear: true, yearRange: range }); }); </script> </head> <body> <input type="text" id="txtDOB" /> </body> </html>Demo:http://jsbin.com/ebevo
raghav_khung...
All-Star
32835 Points
5563 Posts
MVP
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 07:07 AM|LINK
Again the same, choosing what which is difficult to maintain. jQuery Datepicker already has dateformat option.
sunny43
Member
126 Points
170 Posts
Re: How check Date of Birth in Jquery Validation
Nov 14, 2010 07:21 AM|LINK
why I am going for a mask is...
user going to enter the date like 11202010 then I have do some coding to convert that date to 11/20/2010..
mask will automatically adds / after 2 numbers.....
I dont know either this feature is available in datepicker or not..I am very happy to use datepicker but i don't know either i will fit for my requirment or not..
Thanks Raghav...