Time validation with CustomValidator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • frankiefrank
    New Member
    • Jan 2008
    • 15

    Time validation with CustomValidator

    Hey everyone,

    I've been working with the AJAX Control Toolkit (Version 1.0.20229) to make a web form. I used the masked textbox to create two textbox for Time input (HH:MM format). It's supposed to be a range - the Start time textbox value should be smaller than the End time textbos.

    I'm looking for the best way to validate the correct order of the times. I've been trying several approaches - and I'm kind of stuck.

    Any ideas on best practice?
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    1. Make the smaller date the start date, the larger date the end date, however they were delivered.

    2. Or use the start date input to display a menu of selectable dates for the second choice.

    Comment

    • frankiefrank
      New Member
      • Jan 2008
      • 15

      #3
      Thank you mrhoo for the reply,

      but i'm having trouble with your suggestions - perhaps i should have explained my User Interface better.

      I have two textboxes, both need to get Time input. The way I want it to look is as if I have a validator for each control, checking every time I leave the focus of either checkbox if the time range is valid. I wrote the validation function in JavaScript, I just don't know how exactly I should use it

      (for instance, should I implement two custom validators, or should i work with a CompareValidato r? Should I somehow access a validator to display/hide it through JavaScript?

      To your suggestions -
      Originally posted by mrhoo
      1. Make the smaller date the start date, the larger date the end date, however they were delivered.
      The user interface is pretty strict - i have a section with the earlier date control, earlier time control (the one relevant to my question), etc. and likewise for the later date / time and other data. I won't be able to move the value between the textboxes and ignore the way the input looked on the screen originally.

      Originally posted by mrhoo
      2. Or use the start date input to display a menu of selectable dates for the second choice.
      I need the validation for Time, and i need the user to be able to enter any time in both textboxes - just as long as once focus is lost the validation method runs and displays the message if necessary.

      any more suggestions please?

      Thanks,
      Frankie

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        This is not too difficult using simple JavaScript, rather than using a CustomValidator (which I assume is something .NET).

        Just split the hour and minute values from the time value using the split() method, then compare the two hours and minutes, e.g.
        [code=javascript]// time1/2 are textboxes and assume correct format
        var hm = time1.value.spl it(":");
        var hm2 = time2.value.spl it(":");
        var hours = parseInt(hm[0]);
        var mins = parseInt(hm[1]);
        // ...for hm2 too...
        if (hours2 > hours) {// validation fails here...
        ...
        } else if ((hours2 == hours) && (mins2 > mins)) {// fails validation
        ...
        } else return true;[/code]

        Comment

        • frankiefrank
          New Member
          • Jan 2008
          • 15

          #5
          Originally posted by acoder
          This is not too difficult using simple JavaScript, rather than using a CustomValidator (which I assume is something .NET).

          ....
          Thanks acoder - I know how to code the JavaScript logic to check if the hours are valid/invalid.

          What I'm having trouble with is the .NET part - or specifically, how to make it look like a validator behavior, without messing with the other validators.

          It IS something .Net so you'd have to know what validators are / how they work, to help - since that's my main issue.

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            In that case, I suggest you ask in the .NET forum where someone more experienced with .NET controls should be able to help you better.

            Comment

            • frankiefrank
              New Member
              • Jan 2008
              • 15

              #7
              Originally posted by acoder
              In that case, I suggest you ask in the .NET forum where someone more experienced with .NET controls should be able to help you better.
              Ok, I'll repost - thanks

              Comment

              • kho786
                New Member
                • Dec 2008
                • 1

                #8
                Hi everyone I have a validator control for move in dates.
                I cannot figure out why a date in 2009 returns an error move in date invalid.

                I am using javascript. page in question: Houston Apartment Locators | Apartments in Houston | Houston Texas Apartments

                Comment

                Working...