Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# ASP.Net Validate Date

C# ASP.Net Validate Date

This is just a function written in ASP.NET (C#) that validates an input date (with format dd-MM-yyyy) to see if it’s valid. An message will be displayed for each case to indicate the result.

Actually, you can change a bit in the format and separator to validate the given date in other formats such as: dd/MM/yyy, MM-dd-yyyy, MM/dd/yyyy, etc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class csharp_check_valid_date : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    public static bool isValidDate(string datePart, string monthPart, string yearPart)
    {
        DateTime date;
 
        string strDate = string.Format("{0}-{1}-{2}", datePart, monthPart, yearPart);
 
        if (DateTime.TryParseExact(strDate, "dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None, out date))
        {
            return true;
        }
        else
        {
            return false;
        }   
    }
    protected void btnValidate_Click(object sender, EventArgs e)
    {
        string date = txtDate.Text.Trim();
 
        try
        {
            string[] date_arr = date.Split('-');
 
            if (isValidDate(date_arr[0], date_arr[1], date_arr[2]))
            {
                lblMessage.Text = "The given date is VALID!";
            }
            else
            {
                lblMessage.Text = "The given date is NOT VALID!";
            }
        }
        catch(Exception ex)
        {
            lblMessage.Text = ex.Message.ToString();
        }
    }
}

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class csharp_check_valid_date : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } public static bool isValidDate(string datePart, string monthPart, string yearPart) { DateTime date; string strDate = string.Format("{0}-{1}-{2}", datePart, monthPart, yearPart); if (DateTime.TryParseExact(strDate, "dd-MM-yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo, System.Globalization.DateTimeStyles.None, out date)) { return true; } else { return false; } } protected void btnValidate_Click(object sender, EventArgs e) { string date = txtDate.Text.Trim(); try { string[] date_arr = date.Split('-'); if (isValidDate(date_arr[0], date_arr[1], date_arr[2])) { lblMessage.Text = "The given date is VALID!"; } else { lblMessage.Text = "The given date is NOT VALID!"; } } catch(Exception ex) { lblMessage.Text = ex.Message.ToString(); } } }

Mar 16, 2012Hoan Huynh
How to view menu bar in IE, Firefox, Google ChromeManage MacBook Hosts File Easier With Hosts Widget
You Might Also Like:
  • Validate Date With JQuery And Date Object
  • CSharp Validate Internet URL Function With Regular Expression
  • CSharp Validate Email Address Function With Regular Expression
  • SQL Server Get list of dates between start date and end date
  • SQL Server Get number of working days and business hours between start date and end date
  • C# Check File Extension In Array Of Valid Extension
  • String To Upper Case In PHP, JavaScript And .Net (CSharp)
  • String To Lower Case In PHP, JavaScript And .Net (CSharp)
  • Xcode get current date, month, year
  • How To Track Website With Multiple Google Analytisc Accounts
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

10 years ago CSharpASP.NET, DateTime, DateTimeFormatInfo, DateTimeStyles192
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
24,542 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
21,885 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
17,736 views
JQuery Allow only numeric characters or only alphabet characters in textbox
15,059 views
C# Read Json From URL And Parse/Deserialize Json
11,789 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Things to Learn about Installingderm Loan Type S
  • Online Photo Editor – Free Photoediting Software
  • A Guide to Finding the Best Paper Sellers
  • Photoediting in Home Isn’t Hard to Do!

  • Free Photo Editor Online
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (27)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (647)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development