protected bool CheckDate(String date)
{
try
{
DateTime dt = DateTime.Parse(date);
return true;
}
catch
{
return false;
}
}
by using the function You can clear this problem
Eg.
if(CheckDate())
//insert function
else
//Throw error msg
if you want to use by script means try with this,
<script type="text/javascript">
function checkdate()
{
var validformat=/^\d{2}\/\d{2}\/\d{4}$/;
var returnval=false;
var inputvalue=document.getElementById("txtdate").value;
if (!validformat.test(inputvalue))
{
alert("Invalid Date Format. Please correct and submit again.")
}
else
{
var monthfield=inputvalue.split("/")[1]
var dayfield=inputvalue.split("/")[0]
var yearfield=inputvalue.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
{
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.");
}
else
{
returnval=true;
}
}
if (returnval==false)
{
input.select();
return returnval;
}
</script>
by calling
<asp:TextBox ID="txtdate" runat="server" Width="200px"></asp:TextBox>
<asp:Button ID="btnSave" runat="server" OnClientClick="return checkdate(this.txtdate)" onclick="BtnSave_Click"
Text="Save" />
Regards,
Pal