Valid Date

Discussion in 'ASP .Net Web Controls' started by BigJohn, Feb 7, 2007.

  1. BigJohn

    BigJohn Guest

    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
    BigJohn, Feb 7, 2007
    1. Advertisements

  2. 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

    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:

    Hope this helps.


    Steven Cheng

    Microsoft MSDN Online Support Lead


    Get notification to my posts through email? Please refer to

    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


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Feb 8, 2007
    1. Advertisements

  3. BigJohn

    BigJohn Guest

    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
    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",

    <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 =
    if ((clientside_arguments.value.match(RegExPattern)) &&
    (clientside_arguments.value!='')) {
    } else {

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

    Thank you
    BigJohn, Feb 8, 2007
  4. Hello John,

    I've done some research and here are some script code snippet that
    works(actually it is picked modified from the 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);

    <form id="form1" runat="server">
    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" />
    <br />
    <asp:TextBox runat="server" ID="txtDateOfEmploy" MaxLength="10"



    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.


    Steven Cheng

    Microsoft MSDN Online Support Lead

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Steven Cheng[MSFT], Feb 12, 2007
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Matthew Speed
    Nov 8, 2003
  2. Guest
    Scott M.
    Jun 9, 2004
  3. ad
    Brock Allen
    May 4, 2005
  4. Kiran
    Aug 4, 2005
  5. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Michael Borgwardt
    May 30, 2004
  6. Matt
    Pete Becker
    Nov 8, 2003
  7. Matt
    Richard Heathfield
    Nov 8, 2003
  8. Matt
    Aaron Bertrand [MVP]
    Nov 8, 2003