shennyL
shennyL

Reputation: 2794

Regular Expression for Date format: D, dd M yy "Tue, 25 Oct 2011"

I am looking for a regular expression to validation Date selected from a datepicker with format of D, dd M yy

 $('#expirydate').datepicker({
        constrainInput: true, 
        minDate: 0,
        dateFormat: 'D, dd M yy'
    });

And the equivalent format in View is like this:

 <div class="editor-label">
    Expiry Date
</div>
<div class="editor-field">
    @Html.TextBox("ExpiryDate", String.Format("{0:ddd, d MMM yyyy}", DateTime.Now), new { id = "expirydate" })
    @Html.ValidationMessageFor(model => model.ExpiryDate)
</div>

I could not get it in the regex library.. can anyone help?? Appreciate any feedback.. Thanks...

Upvotes: 1

Views: 680

Answers (2)

Kevin Collins
Kevin Collins

Reputation: 1461

If it's coming from a datepicker, and you trust that it won't produce impossible dates, then to validate that format you could use:

(Sun|Mon|Tue|Wed|Thu|Fri|Sat|Sun), [1-3]{1}[0-9]{0,1} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{4}

Upvotes: 1

Jeff Ferland
Jeff Ferland

Reputation: 18292

Well, I wrote a Regex for all the months, started to validate the days, and realized that you have to validate 28-31 depending on the month and year anyway.

Given that, you should skip the Regex and start looking down the road of looking at the MSDN documentation on parsing data and time strings.

If you really want to pre-check it with a regex, (Mon|Tue|Wed|Thu|Fri|Sat|Sun), (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-3]\d) (\d{4}) is the the basic setup.

Upvotes: 0

Related Questions