Ajax Calendar and its Validation


Calendar control is usually used to pick up date when you click or tab on the text box.I used Ajax control kit calendar control.The below code describe how to use Ajax calendar control and its validation at server side.Validation i am doing here is,the date should be greater than current date and contains current "MM/dd/yyyy" format.This is exclusively helpful for beginners who are new to Ajax calendar controls.

Download AjaxControlToolkit dll and add it to your website references first before going further.

First create a new website and give its name as CalendarSample.In the default.aspx,design page.


1.Register an assembly with name AjaxControlToolKit as shown below..


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>


2.Copy the below code script in the head tag as shown below




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

function checkDate(sender, args) {
if (sender._selectedDate < new Date()) {

alert("Date should be greater than today");

sender._selectedDate = new Date();
// set the date back to the current date
sender._textbox.set_Value(sender._selectedDate.format(sender._format))

}
}
</script>





3.i have attached the code which should be copied in between form tag in html page.

4.In the cs file i.e. code behind file.Copy the below code to validate the date.


protected void btnSubmit_Click(object sender, EventArgs e)
{
if (!ValidateDate(txtExpDt.Text.Trim()))
{
//Please display an alert with Page.RegisterStartupScript as Date is invalid.....


txtExpDt.Text = "";
txtExpDt.Focus();
return;
}
}


private bool ValidateDate(string val)
{
bool retValue = true;
try
{
DateTime dt = Convert.ToDateTime(val);
DateTime bDay = Convert.ToDateTime(val.Trim());
if ((bDay < DateTime.Now) || (bDay < Convert.ToDateTime("01/01/1960")))
{
lblMessage.Text = "Invalid Date";

retValue = false;
}
if (Convert.ToDateTime(val.Trim()) < DateTime.Now)
{
lblMessage.Text = "Invalid Date,Please Enter Valid Date";
retValue = false;
}
else
{

}

}
catch (Exception ex)
{
retValue = false;
}
return retValue;
}


Attachments

  • Ajax Calendar and its Validation (42803-271919-formtag.txt)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: