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
    1. Advertising

  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
    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.
     
    Steven Cheng[MSFT], Feb 8, 2007
    #2
    1. Advertising

  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
    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
     
    BigJohn, Feb 8, 2007
    #3
  4. 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" />
    <br />
    <asp:TextBox runat="server" ID="txtDateOfEmploy" MaxLength="10"
    ></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.
     
    Steven Cheng[MSFT], Feb 12, 2007
    #4
    1. Advertising

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
    Replies:
    1
    Views:
    646
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,365
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    566
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    755
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    426
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page