Time validation in a text box

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dougancil
    Contributor
    • Apr 2010
    • 347

    Time validation in a text box

    I'm trying to use required validation controls on two text boxes and tried using the following code:

    Code:
    <asp:TextBox ID="starttimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
              <span class="style1">*</span> Start Time  <asp:RequiredFieldValidator ValidationExpression="^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$"
                ID="StartTimeValidator" runat="server" 
                ErrorMessage="You Must Supply Both a Start and an End Time" 
                ControlToValidate="starttimeInput"></asp:RequiredFieldValidator>
    but I can still enter whatever I want into the text box. What I'm trying to check for is that users are entering a time in a valid format.
    Can someone please point out to me where my expression may be wrong?

    Thank you



    Doug
  • orked
    New Member
    • Jan 2009
    • 49

    #2
    Hello,
    you need Regular Expression validator to check on the input from user you can change validation expression on it's properties you can write this value on it
    [0-2][0-3]:[0-5][0-9]
    Try this i think it will help you.

    Orked

    Comment

    • dougancil
      Contributor
      • Apr 2010
      • 347

      #3
      Orked,

      Yes sorry I was using the wrong kind of validator. My next question is in relation to this. I'm noticing that when I move from field to field that the validator checks for correct information in the text fields, is there a way that I can just have the validation run when a user presses the submit button rather than as they are typing?

      Comment

      • orked
        New Member
        • Jan 2009
        • 49

        #4
        Doug,

        you can make the enable state to false in the properties of validator and change this state in the code behind the button you want to click on it like this
        Code:
        protected void Button1_Click(object sender, EventArgs e)
            {
                RegularExpressionValidator1.Enabled = true;
                
            }
        Orked

        Comment

        • dougancil
          Contributor
          • Apr 2010
          • 347

          #5
          Ok last question on this subject,

          I'm noticing that when I try to validate for time in one of my text boxes that I have the following regular expression:

          ValidationExpre ssion="^([0-1][0-9]|[2][0-3]):([0-5][0-9])$"

          What that doesnt cover of course is if someone just enters in say

          1:00 .. it returns an error.

          What's the best way around accepting both and 1 and 2 characters for time?

          Would it be something similar to date?

          "^\d{1,2}\/\d{1,2}\/\d{4}$"

          Thanks

          Doug

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Your first regular expression will work with the addition of a "?". The ? indicates that there should be 0 or 1 repetitions of something.

            So, it's pretty easy to modify your first regular expression to this so that it includes the cases where no leading 0 is provided:

            ^([0-1]?[1-9]|2[0-3]):[0-5][0-9]$

            -Frinny

            Comment

            • dougancil
              Contributor
              • Apr 2010
              • 347

              #7
              Ok so I had to do some more testing on this but I have the following 3 text boxes:

              Code:
                      <asp:TextBox ID="exceptiondateInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
                      &nbsp; <span class="style1">*</span> Exception Date &nbsp;<asp:RegularExpressionValidator 
                          ID="DateValidator" runat="server" 
                          ControlToValidate="exceptiondateInput"
                          ValidationExpression="^\d{1,2}\/\d{1,2}\/\d{4}$" 
                          ErrorMessage="Please Enter the Date Correctly"></asp:RegularExpressionValidator>
                      <br />
                      <br />
                      <br />
                      <asp:TextBox ID="starttimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
                      &nbsp; <span class="style1">*</span> Start Time &nbsp;<asp:RegularExpressionValidator
                          ValidationExpression="^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$"
                          ID="StartTimeValidator" runat="server" 
                          ErrorMessage="You Must Supply an Start Time" 
                          ControlToValidate="starttimeInput"></asp:RegularExpressionValidator><br />
                      <br />
                      <br />
                      <asp:TextBox ID="endtimeInput" runat="server" Width="130px" Height="23px"></asp:TextBox>
                      &nbsp; <span class="style1">*</span> End Time &nbsp;<asp:RegularExpressionValidator 
                          ID="EndTimeValidator" runat="server" 
                          ValidationExpression="^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$"
                          ErrorMessage="You Must Supply an End Time" 
                          ControlToValidate="endtimeInput"></asp:RegularExpressionValidator>
              and if I hit my submit button here without entering anything in those textboxes, I don't get an error. What am I missing here?

              Comment

              • stefbek97
                New Member
                • Aug 2009
                • 54

                #8
                RequiredFieldVa lidator : Will make sure the control is not blank.

                ReqularExpressi onValidator : Will make sure the input is Valid.

                You Need Both Per TextBox..

                Comment

                Working...