Valid Date

B

BigJohn

I want to use a Validator control to ensure a date field has a valid date in
it. I tried finding the ValidationExpression to use in a
RegularExpressionValidator but to no avail. If CustomValidator is the
solution, can I have a pointer to a good date validation example.

Thank you
 
S

Steven Cheng[MSFT]

Hello John,

As for date/time validation, if you want to do it on client-side, then
script is necessary. And based on my experience, in most cased, people will
chose to use regular expression for validate different kind of date/time
formats. for your scenario, you may need to determine what kind of format
you will allow in your form's input. Here are some existing regex
expressions for date/time validation:

#Date &Time patter in RegexLib
http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5

http://www.expertsrt.com/scripts/Rod/validate_date.php

and if you do not use regex, you can manually get the data in html input
box and parse it as normal string to validate it. Here are some web
articles which has provide some sample code on validate date/time input
through javascript:


http://www.mattkruse.com/javascript/date/

http://www.weberdev.com/get_example-353.html

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
B

BigJohn

Thank you for your response, the link were very helpful.

I am going with the client side JavaScript since it will work on all public
web client machines.

I am using the code from
http://www.expertsrt.com/scripts/Rod/validate_date.php
in conjunction with a CustomValidator.

The usage example in MSDN Library uses VBScript on the client, but I am
having a problem translating to JS. Here is what I have currently:

<asp:TextBox Width=100 runat=server ID="txtDateOfEmploy" MaxLength="10"
Text='<%#DataBinder.Eval(Container.DataItem, "dteDateOfEmploy",
"{0:d}")%>'></asp:TextBox>

<asp:CustomValidator ID="CustomValidator2" runat="server"
ValidateEmptyText=false ControlToValidate="txtDateOfEmploy"
ClientValidationFunction="ValidateDate" ErrorMessage=<%#
FormatValidationTitleE1(databinder.eval(container.dataitem, "strType")) & "
Date of Employment format is not valid. Should be a valid date with format:
MM/DD/YYYY" %>>*</asp:CustomValidator>

<script type="text/javascript">
function ValidateDate(Source, clientside_arguments) {
var RegExPattern =
/^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\
(?=\d)))?(((0?[1-9]|1[012]):)[0-5]\d){0,2}(\
[AP]M))|([01]\d|2[0-3]):)[0-5]\d){1,2})?$/;
if ((clientside_arguments.value.match(RegExPattern)) &&
(clientside_arguments.value!='')) {
return(true)
} else {
return(false)
}
}
</script>

I think my problem resides in not knowing what exactly to return, if anything.

Thank you
 
S

Steven Cheng[MSFT]

Hello John,

I've done some research and here are some script code snippet that
works(actually it is picked modified from the asp.net regular expression
validator's client output code). Here is the script function and the html
elements on the test page:

=========================
.............................
function validate()
{
alert("validation started....");

var input = document.getElementById("txtInput").value;
var rx = new RegExp(document.getElementById("txtPattern").value);
var matches = rx.exec(input);


var result = (matches != null && input == matches[0]);

alert("result: " + result);

}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Validation Pattern: <textarea name="txtPattern" id="txtPattern"
value="^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$" rows="3" cols="30" ></textarea>
<br />Input Data: <input type="text" name="txtInput" id="txtInput" />

<input type="button" value="do validation"
onclick="javascript:validate();" />

<br /><br /><br />
<hr /><br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</asp:TextBox>
....................

======================================

I explicitly create a RegExp jscript object and assign the regular
expression(Pattern) into its constructor. and call its "exec" method
against the text (you want to validate) will return a matches list, you can
check whether it is null or whether the first item is your input text to
determine whether the vaildation succeed.

This works with some test regular expressions. However, for the patter
regex you provided, I always get validation failure, I'm not sure whether
there is any typo in the expression or if i haven't input the correct
datetime value :)

Anyway, hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top