regularExpressionValidator and dateTime

  • Thread starter Thread starter Ben
  • Start date Start date
B

Ben

Hi,

i would like to find a validationexpression for a
regularexpresssionvalidator for DateTime from 1900 like thsi:
dd/mm/yyyy (european).

I couls find some at http://regexlib.com
but strange enough, all the validation i found there failed when typing a
year before 2000 (e.g. 1999)

Thanks
Ben
 
Good evening Ben,

Use RangeValidator instead:

<asp:TextBox runat="server" ID="txtDate" />
<asp:RangeValidator Type="Date" runat="server" ID="rv"
ErrorMessage="Please enter a valid date greater than 01/01/1900"
EnableClientScript="true" ControlToValidate="txtDate"
MinimumValue="01/01/1900" MaximumValue="01/01/2999"
Display="Dynamic" CultureInvariantValues="false" />
<asp:RequiredFieldValidator runat="server" ID="rfv"
ErrorMessage="Please enter a date in the DD/MM/YYYY format"
ControlToValidate="txtDate" Display="Dynamic"
EnableClientScript="true" /><br />
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en-gb");
}
</script>

Hope this helps
 
Hello,

If the format that you need is exactly dd/mm/yyyy and the year greater
than 1900, you can use the following RegEx:


^\d\d/\d\d/(19|2\d)\d\d$

there you say, two digits for days, two digits for month, and 19XX or
2XXX for year (^and $ is to say I want exactly this full string, no
substrings patterns).

You can find a good free tool to check regular expression:

http://www.codeproject.com/dotnet/expresso.asp

HTH
Braulio

--
/// ------------------------------
/// Braulio Díez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
Thanks to both of you

Braulio Diez said:
Hello,

If the format that you need is exactly dd/mm/yyyy and the year greater
than 1900, you can use the following RegEx:


^\d\d/\d\d/(19|2\d)\d\d$

there you say, two digits for days, two digits for month, and 19XX or
2XXX for year (^and $ is to say I want exactly this full string, no
substrings patterns).

You can find a good free tool to check regular expression:

http://www.codeproject.com/dotnet/expresso.asp

HTH
Braulio

--
/// ------------------------------
/// Braulio Díez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
Hi Braulio,

Unfortunatelly, regex you have given tests lexical validity only, whilst he
requires logical date validation as well (number of days in a month is not
fixed, leap years etc)

regards
 
Back
Top