I'm developing a project using Entity framework in .NET 4
. There is a page for sign up which has a field of date of birth. I want to validate it so that date of birth cant be in future or today. Moreover if it could be 5 years back as pared today.
Here is my code.
Date of Birth:
<asp:RequiredFieldValidator ID="rfv_txtbx_DOB" runat="server" ControlToValidate="txtbx_DOB" CssClass="validator" Display="Dynamic" ErrorMessage="Date of Birth Required" SetFocusOnError="True" Text="*" ValidationGroup="vg" />
<asp:CustomValidator ID="cv_txtbx_DOB" runat="server" ControlToValidate="txtbx_DOB" CssClass="validator" Display="Dynamic"
ErrorMessage="Date of Birth cannot be today or in future" SetFocusOnError="True" Text="*" ValidationGroup="vg" ClientValidationFunction="validateDate"/>
<asp:TextBox ID="txtbx_DOB" runat="server" CssClass="txtbx" Width="200px" />
<ajaxToolkit:CalendarExtender ID="txtbx_DOB_CalendarExtender" runat="server" Enabled="True" Format="dd-MM-yyyy" TargetControlID="txtbx_DOB" />
<script language="javascript" type="text/javascript">
function Validate(sender, args) {
var currentDate = new Date().getDate();
if (args.Value < currentDate)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
I'm developing a project using Entity framework in .NET 4
. There is a page for sign up which has a field of date of birth. I want to validate it so that date of birth cant be in future or today. Moreover if it could be 5 years back as pared today.
Here is my code.
Date of Birth:
<asp:RequiredFieldValidator ID="rfv_txtbx_DOB" runat="server" ControlToValidate="txtbx_DOB" CssClass="validator" Display="Dynamic" ErrorMessage="Date of Birth Required" SetFocusOnError="True" Text="*" ValidationGroup="vg" />
<asp:CustomValidator ID="cv_txtbx_DOB" runat="server" ControlToValidate="txtbx_DOB" CssClass="validator" Display="Dynamic"
ErrorMessage="Date of Birth cannot be today or in future" SetFocusOnError="True" Text="*" ValidationGroup="vg" ClientValidationFunction="validateDate"/>
<asp:TextBox ID="txtbx_DOB" runat="server" CssClass="txtbx" Width="200px" />
<ajaxToolkit:CalendarExtender ID="txtbx_DOB_CalendarExtender" runat="server" Enabled="True" Format="dd-MM-yyyy" TargetControlID="txtbx_DOB" />
<script language="javascript" type="text/javascript">
function Validate(sender, args) {
var currentDate = new Date().getDate();
if (args.Value < currentDate)
args.IsValid = true;
else
args.IsValid = false;
}
</script>
Share
edited Apr 1, 2013 at 12:05
ssilas777
9,7624 gold badges47 silver badges69 bronze badges
asked Apr 1, 2013 at 11:51
Ali UmairAli Umair
1,4241 gold badge22 silver badges45 bronze badges
1
- Here is a uesful link stackoverflow./questions/6133539/… – Ratna Commented Apr 1, 2013 at 11:58
2 Answers
Reset to default 3function check_date()
{
var chkdate = document.getElementById("ID OF YOUR TEXT FIELD HERE").value;
var edate = chkdate.split("/");
var spdate = new Date();
var sdd = spdate.getDate();
var smm = spdate.getMonth();
var syyyy = spdate.getFullYear();
var today = new Date(syyyy,smm,sdd).getTime();
var e_date = new Date(edate[2],edate[1]-1,edate[0]).getTime();
if(e_date > today)
{
alert("Date is not valid");
return false;
}
}
On page load
protected void Page_Load(object sender, EventArgs e)
{
Calendar1.EndDate = DateTime.Now; //to dissable future Date
}
Here is a link with all the restriction in calendar extender
http://www.karpach./ajaxtoolkit-calendar-extender-tweaks.htm
Or use the following java-script function
function checkDate(sender, args) {
if (sender._selectedDate > new Date()) {
alert("You can select a day earlier than today!");
sender._selectedDate = new Date();
// set the date back to the current date
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}