Forum guidelines
Validate Date
Posted By richard smith on 04 Feb 2013
I have a text box on a web form where a user will input a Discharge Date...The date MUST be entered in this format MM/DD/YYYY what C# code would validate that the entry is input in that format, and if not show an error?  Something like:
//check to see if the date is correct format
//how to check?
//if valid Response.Redirect -- Go to next page
Error.Text = "Dates must be entered in a MM/DD/YYYY Format.  Please check the entry and try again."

  • 1
  • 0
Re: Validate Date
Posted By Vulpes on 04 Feb 2013   Accepted Answer
I's use something like this:

      DateTime dt;
      if (DateTime.TryParseExact(textBox1.Text, "MM/dd/yyyy", null, 0, out dt))
         // it's OK and result is in 'dt'
         // it's not OK

Re: Validate Date
Posted By richard smith on 04 Feb 2013  
When I add that syntax, I get an error of:
The best overloaded method match for System.DateTime.TryParseExact(string, string, SystemIFormatProvider, System.Globalization.DateTimeStyles, out System.DateTime) has some invalid arguments.

Re: Validate Date
Posted By Satyapriya Nayak on 04 Feb 2013  
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Validate_Date._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
  function dateValidation()

var obj = document.getElementById("<%=txtDate.ClientID%>");

var day = obj.value.split("/")[0];
var month = obj.value.split("/")[1];

var year = obj.value.split("/")[2];
if ((day<1 || day >31) || (month<1&&month>12)&&(year.length != 4))

alert("Invalid Format");return false;



    <form id="form1" runat="server">
    <asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" OnClientClick="dateValidation()" Text="Button" />

If this post helps you mark it as answer

Re: Validate Date
Posted By richard smith on 04 Feb 2013  
Lost me on that post.  Where does that coding go?  I don't think it is C# coding...

I am assuming that is javascript, and I need it to be C#

Re: Validate Date
Posted By Vulpes on 04 Feb 2013  
Are you sure that you've copied it down correctly as it's working OK for me?

Note in particular that 'dt' should be preceded by 'out' as it's an output parameter.

You can replace '0' with DateTimeStyles.None if you want to be more verbose.

Re: Validate Date
Posted By Suthish Nair on 04 Feb 2013  
 A small change... But why just use a date control.

 DateTime dt1;
 if (DateTime.TryParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt1))
    Console.WriteLine("it's OK and result is in 'dt'");
     Console.WriteLine("it's not OK");

Answer helped you, Mark as Answer?
Re: Validate Date
Posted By Vulpes on 05 Feb 2013  
When you pass 'null' for that particular parameter, then the current culture is used (en-US in Richard's case).

So, with a simple date format such as MM/dd/yyyy, it doesn't matter whether you use that or the Invariant culture.

C# Corner Annual Conference 2016


Spire.Doc - Free .NET Word API
Use Spire.Doc to create, read, write, print, and convert word documents to OpenXML, RTF, TXT, XPS, EPUB, EMF, HTML, Image and PDF.