Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
Hi
I have two text boxes in which the start date and end date is to be filled.The date, month can be different but the year should be same.So i just want to put a validation for the user that they should put the same year else a error message generate showing the year is not matching.
Posted 30-Dec-12 19:07pm
falque303
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Try this..
    protected void Button1_Click(object sender, EventArgs e)
    {
        compareDate();
    }
    void compareDate()
    {
        DateTime year1 = DateTime.Parse("13/8/2010"); //Date Format: dd/MM/yyyy
        DateTime year2 = DateTime.Parse("9/2/2012"); //Date Format: dd/MM/yyyy

        if (year1.Year != year2.Year)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "msg", "alert('Year is not matching!');", true);
        }
    }
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 31-Dec-12 1:34am
   
Basically, that's it, my 5. And if the time data is written in a different format, there are different forms of Parse or TryParse.
—SA
ridoy at 31-Dec-12 1:41am
   
+5,but SA is right..TryParse would be a good one
prashant patil 4987 at 31-Dec-12 1:44am
   
ya... i agree with both of you SA and ridoy.. try parse is also good for parsing.
falque at 31-Dec-12 7:48am
   
Thanks for the concern.Its working fine now but there is one more requirement as when the user put the value in the second text box it automatically compares the value of year in both the text boxes if its similar its ok else produce an error message showing the years are not mathching. Any help would be appreciated.Thanking You
prashant patil 4987 at 1-Jan-13 0:24am
   
@faique: can you clear your quetion?
falque at 1-Jan-13 2:11am
   
On my page there are two text boxes for the user to fill start date and the end date the days and month can be different but the year should be the same.So i have put one validation for checking years on a submit button and its working fine.But now i want the event should be changed when the user fills the second textbox it checks in background and gives the error message without clicking on submit button.Something like autosearch in jquery. Any help would be appreciated.Thanking You
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You have to first tell the user the format of date they will have to enter in the text box (such as 'dd/mm/yyyy'). Its best to instead use a DateTimePicker control instead of textbox because then the user will click and choose the date where as if the user enters a date in a textbox they can make a mistake.
 
if let's say you are using a textbox, then in the event handler method where you are using the input you can use the following code:
 
DateTime dateTime1 = DateTime.Parse(textBox1.Text); //Where I suppose the user enters "dd/mm/yyyy" format of date
DateTime dateTime2 = DateTime.parse(textBox2.Text); //Second input of date value

//Comparing them
if (dateTime1.Year != dateTime.Year)
{
    //Some action
}
  Permalink  
Comments
falque at 31-Dec-12 8:15am
   
Thanks for the concern.Its working fine now but there is one more requirement as when the user put the value in the second text box it automatically compares the value of year in both the text boxes if its similar its ok else produce an error message showing the years are not mathching. Any help would be appreciated.Thanking You
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can use like this for getting year from textbox data & then can compare easily...
 
DateTime dt = "xx/xx/xxxx";//your date Convert.ToDate(TextBox1.Text)
int year1 = dt.Year;
 
Thanks
Developers Blog[^]
Hemant Singh Rautela
  Permalink  
Comments
falque at 31-Dec-12 2:51am
   
Thanks for great help but whatever date format i m putting its giving an error message showing "String was not recognized as a valid DateTime".Please provide the solution.Thanking you
hemantrautela at 31-Dec-12 5:20am
   
you can use CalenderExtender control of ajaxtoolkit for picking date, Then it works perfectly. Default datetime format is (mm/dd/yyyy)
 

& if you like solution then vote plz...

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



Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100