Geeks With Blogs
Gaurav Taneja Great dreams... never even get out of the box. It takes an uncommon amount of guts to put your dreams on the line, to hold them up and say, "How good or how bad am I?" That's where courage comes in.

     Javascript Function for  date validation

 

function checkDate() {

    var myDayStr = document.getElementById('ddlDays').value;

    var myMonthStr = parseInt(document.getElementById('ddlMonth').value)-1;

    var myYearStr = document.getElementById('ddlYear').value;

    var dteDate;

 

    dteDate=new Date(myYearStr,myMonthStr,myDayStr);

 

    var valid= ((myDayStr==dteDate.getDate()) && (myMonthStr==dteDate.getMonth()) && (myYearStr==dteDate.getFullYear()));

    var myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;

    if(valid==false)

    {

        alert( 'Selected date is not valid.' );

    }

    return valid;

    }

 

IF CONTENTPLACE HOLDER ISTHERE

----------------------------------

<script language="javascript" type="text/javascript">

function checkDate() {

    var myDayStr = document.getElementById('ctl00_ContentPlaceHolder1_ddlDays').value;

    var myMonthStr = parseInt(document.getElementById('ctl00_ContentPlaceHolder1_ddlMonth').value)-1;

    var myYearStr = document.getElementById('ctl00_ContentPlaceHolder1_ddlYear').value;

    var dteDate;

 

    dteDate=new Date(myYearStr,myMonthStr,myDayStr);

 

    var valid= ((myDayStr==dteDate.getDate()) && (myMonthStr==dteDate.getMonth()) && (myYearStr==dteDate.getFullYear()));

    var myDateStr = myDayStr + ' ' + myMonthStr + ' ' + myYearStr;

    if(valid==false)

    {

        alert( 'Selected date is not valid.' );

    }

    return valid;

    }

     </script>

 

 

ON PAGE LOAD

  (ON CS)btnSave.Attributes.Add("OnClick", "javascript:checkDate();");

OR

 (ON ASPX)OnClientClick="javascript:return checkDate();

Methods for dropdown for date & year.

/// <summary>

    /// Method to Get date in dropdown.

    /// </summary>

    public void GetDate()

    {

        for (int Day = 1; Day <= 31; Day++)

        {

            ListItem li = new ListItem();

            li.Text = Day.ToString();

            li.Value = Day.ToString();

            ddlDays.Items.Add(li);

        }

        ddlDays.Items.Insert(0, new ListItem("Select Date", ""));

    }

 

    /// <summary>

    /// Method to fill year in dropdown.

    /// </summary>

    public void GetYear()

    {

        for (int Year = 1900; Year < 2010; Year++)

        {

            ListItem li = new ListItem();

            li.Text = Year.ToString();

            li.Value = Year.ToString();

            ddlYear.Items.Add(li);

        }

        ddlYear.Items.Insert(0, new ListItem("Select Year", ""));

 

    }

 

CS CODE FOR FETCHING BACK DATE INTO DROPDOWNS

-------------------------------------------------

string Date = (dsProfile.Tables[0].Rows[0][MyTioMember.KEY_DATA_DOB].ToString());

                        string[] DateValues = new string[3];

                        char[] splitter = { '/' };

                        DateValues = Date.Split(splitter);

Posted on Thursday, April 3, 2008 8:06 PM Vini | Back to top


Comments on this post: Javascript Function for date validation

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Gaurav Taneja | Powered by: GeeksWithBlogs.net