Forum guidelines
AuthorQuestion
Validate Date
Posted 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:
ButtonFinish_Click()
{
//check to see if the date is correct format
try
{
//how to check?
//if valid Response.Redirect -- Go to next page
}
catch
{
Error.Text = "Dates must be entered in a MM/DD/YYYY Format.  Please check the entry and try again."

AuthorReply
Vulpes
  • 1
  • 0
accepted
Re: Validate Date
Posted 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'
      }     
      else
      {
         // it's not OK
      }

Re: Validate Date
Posted 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 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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<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;

}

}

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



http://paulschreiber.com/blog/2007/03/02/javascript-date-validation/

If this post helps you mark it as answer
Thanks

Re: Validate Date
Posted 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 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 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'");
  }
  else
  {
     Console.WriteLine("it's not OK");
   }

Mark as Answer, on the posts reply's that helped you !!!.
Re: Validate Date
Posted 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.

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.