开发者

How to check whether the entered date is in the correct format?

I have a textbox where a user is supposed to enter a date. How can I check whether the entered date is in the 开发者_C百科correct date format?

Like this: 02/02/2008

  • not like this 022/22/-1
  • not like this 2009/02/02
  • not like this 02/Jun/2015
  • not like this 02/abc/2010

(I don't want to use DateTimePicker or MonthCalender).

EDIT 1

I tried it like this

Private Sub txtHireDate_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtHireDate.Validated
    Dim dateString As String = txtHireDate.Text
    Dim formats As String = "MM/dd/yyyy"
    Dim dateValue As DateTime

    If DateTime.TryParseExact(dateString, formats, New CultureInfo("en-US"), DateTimeStyles.None, dateValue) Then

    End If

But its showing some errors

How to check whether the entered date is in the correct format?


Use DateTime.TryParseExact function:

String dateString = dateTextBox.Text;
String formats = "MM/dd/yyyy";
DateTime dateValue;

if (DateTime.TryParseExact(dateString, formats, 
                              new CultureInfo("en-US"), 
                              DateTimeStyles.None, 
                              out dateValue))
{
    // That means the value of the date is in the specified format..
}

Dont forget to use the Globalization namespace:

using System.Globalization;


I use the following regexp in a FormatValidator control under the textbox:

^(19|20)\d\d[- /.]?(0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])$

This is in year-month-day format, and allows for the following formats:

  • yyyy-MM-dd
  • yyyyMMdd
  • yyyy/MM/dd
  • yyyy.MM.dd

Note that year month day format cannot be confused when working with standard north american dates. American format (MM/dd/yyyy) will parse differently than Canadian format (dd/MM/yyyy), but you won't know it's wrong until you hit days higher than 12...

Example of adding a validator to the page:

<asp:RegularExpressionValidator ID="txtTreatmentDate_FormatValidator" runat="server" 
    Display="Dynamic" ValidationGroup="LogSave"
    ControlToValidate="txtTreatmentDate" ValidationExpression="ThisIsIgnored"
    ErrorMessage="*Invalid Date" />

Then in your code behind (C#)

protected void Page_Load(object sender, EventArgs e)
{
    txtTreatmentDate_FormatValidator.ValidationExpression = DateHandler.DateFormatStandardRegex;
}

In DateHandler (with time format as well, if you need it):

public static readonly String DateFormatStandardRegex = "^(19|20)\\d\\d[- /.]?(0[1-9]|1[012])[- /.]?(0[1-9]|[12][0-9]|3[01])$";
public static readonly String TimeFormat24HourRegexp = "(20|21|22|23|[01]\\d):?([0-5]\\d):?([0-5]\\d(\\.\\d*)?)?";

Since you didn't specify the format you were looking for (I assume US) I didn't modify this to work for you, but it's a simple matter of moving the blocks around and changing the allowed delimiters.

Canadian Format:

"^(0[1-9]|[12][0-9]|3[01])[/](0[1-9]|1[012])[/](19|20)\\d\\d$";

When parsing the date, you'll definitely want to use ParseExact with EN-CA (I think that's the short form) culture, or specify the format string you want the date to comply to (i.e. "dd/MM/yyyy")

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜