Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All

I'm new to this forum and this one is my very first query.

I've a small form in ASP.NET (visual studio) code behind lang VB.

On this form there are two txtboxes and other controls too.

Let txt1 & txt2, both contain Date
What I need to validate is;

txt1 <= txt2 <= Today's

Help
Posted
Comments
Hemant Singh Rautela 12-Apr-13 5:56am    
It is very simple code, if you cannot write that code its mean you have not basic knowledge of any language & data structure..
so my suggestion is that first learn both things(any language & data structure)... :-) This forum is not as a school where someone teach you how to write a code....
Rajiv Kr. Thakur 13-Apr-13 9:14am    
@hemantrautela - Dear, if you have the knowledge then you must have the time to share it with others. And, if I know about what I've asked then probably I will not ask. Many things that I know are very simple for me and others are not. Thanks anyway.

Use this:-

C#
function BirthdayValidator(txtDOB)
{
    var userDate = txtDOB.value
    var today = new Date();            // get today's date
    uDt = userDate.split("/");          //get user date
    var uDate = new Date(uDt[2], uDt[1] - 1, uDt[0]);
    if (uDate>today)
    {
        alert("'Date Should be less than Today's Date.");
        txtDOB.value="";
        txtDOB.focus();
        return false;
    }
}
 
Share this answer
 
you can use an asp:CompareValidator.

as like

1.
C#
<asp:comparevalidator id="compvalidDtGft" runat="server" cssclass="red" forecolor=" " controltovalidate="txt2" controltocompare="txt1" xmlns:asp="#unknown">
                                          ErrorMessage="Date selected cannot be greater than first date." Operator="LessThanEqual" Type="Date"
                                          Display="Dynamic"></asp:comparevalidator>


and in addition to this

2.
C#
<asp:comparevalidator id="CompareValidator1" runat="server" controltovalidate="txt2" xmlns:asp="#unknown">

ErrorMessage="CompareValidator" Operator="GreaterThanEqual" Type="Date"></asp:comparevalidator>


And Try to put this on page_load event

C#
protected void Page_Load(object sender, EventArgs e)

{

CompareValidator1.ValueToCompare = DateTime.Now.ToShortDateString();

}



or try

2.

C#
<asp:comparevalidator id="CompareValidator1" runat="server" controltovalidate="txt2" valuetocompare="<%= DateTime.Now.ToShortDateString() %>" xmlns:asp="#unknown">

ErrorMessage="CompareValidator" Operator="GreaterThanEqual" Type="Date"></asp:comparevalidator>



IF all these doesn't work try to write a custom validator
 
Share this answer
 
VB
<asp:RangeValidator ID="valrDate" runat="server" ControlToValidate="txtDob" MinimumValue="01/01/1940"
                       MaximumValue="31/12/1994" Type="Date" Text="This Date Could not be a Birth Date"
                       Display="Dynamic" Font-Size="Smaller" ValidationGroup="Submit" />
 
Share this answer
 
Thanks to all for answering my query and to all commenting too. Applying two asp:comparevarlidator solved my problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900