Validate time where start time should be less than end time

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • narinas
    New Member
    • Jan 2012
    • 20

    Validate time where start time should be less than end time

    Hello im using dhtmlx grid in this cells i have from time and to time (i.e ind==1 and ind==2) i need to validate time where to-time must be greater than from-time else it should alert an message,i have writeen a normal Regular expression for the time validation.as the time is i string format i.e[05:00],please can any one send me code for that.thank you

    Code:
    var err_str="";
    function validate_grid(value,id,ind)
    	{
    	if(ind==1 || ind==2)
    		{
    			var patt=/^([0][0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/;
    			if(!patt.test(value))
    			{
    				mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
    				if(err_str!="")
    					err_str+="\n"+(ind+1)+".Enter numbers and : only..";
    				else
    					err_str=(ind+1)+".Enter numbers and : only";
    				return false;
    			}
    			else
    			{
    				mygrid.setCellTextStyle(id,ind,"background-color:white;");
    				return true;
    			}
    		}
    }
  • C CSR
    New Member
    • Jan 2012
    • 144

    #2
    Are you saying your parameter for time is a string? Was it always a string or did you convert it before you passed it to this function?

    How is it "gathered"( user input?, database?, list?).

    For good measure, please list the values and types of the arguments (value, id, ind) for this function, and , show the types before and after any conversion was made prior to this function call.

    That may be helpful in providing a shortcut to your issue.

    Comment

    • C CSR
      New Member
      • Jan 2012
      • 144

      #3
      And I forgot: Are you using AM & PM with these values?

      Comment

      • narinas
        New Member
        • Jan 2012
        • 20

        #4
        Actually the concept is dhtmlxgrid http://docs.dhtmlx.com/doku.php?id=d...ing#validation
        im not checking AM and PM,its 24hr format,im checking regular expression for that ind==1 and 2

        Comment

        • C CSR
          New Member
          • Jan 2012
          • 144

          #5
          I went to the site and it did not explain the type of values it contains. I have to assume they are put there as strings from wherever they come from. The problem with that is we can't compare their values as is for several reasons. But here's two reasons:

          1) As strings, 12 < 5 and 22 < 19. (based on the first chars)

          2) We have to make sure they are in a "controllab le" format, even if it is a string (same size and shape). That is because we would need to perform some operations on them to create "comparable " values. Otherwise the code gets messy.

          Also, if you are planning on doing this inside the function you sent, I only see one value coming in! If I'm wrong, clearly explain to me how.

          The only nice way to compare date/time strings is if they can be converted to a Date/Time variable "type." Then we can evaluate them within a few steps.

          Any more information or ideas?

          Comment

          • C CSR
            New Member
            • Jan 2012
            • 144

            #6
            Correction: 22 is not less than 19, but you see what I mean :)

            Comment

            • C CSR
              New Member
              • Jan 2012
              • 144

              #7
              Here's one possibility:

              Get the length of the string, so that if its less that 5 add a zero (0) to the front. You get both strings with a length of 5. If there all in this shape ( hh:nn ) then you can compare them with:

              example:
              x = "5:00"; y = "12:00"; length of x is < 5, so x = "0" & x;

              now you have:
              x = "05:00"; y = "12:00";

              In terms of strings, x would be less than y, as it should be. Note: if these string cross over into 2 separate days, it messes it up without the AM & PM designators to test. What do you think?

              Comment

              • narinas
                New Member
                • Jan 2012
                • 20

                #8
                yes if i take 08:00 and 8:00 its showing error.look at this code
                Code:
                <script type = "text/javascript">
                
                var fromTime = "05:50";  // format hh:mm
                var toTime = "06:21";
                var ft = fromTime.split(":");
                var tt = toTime.split(":");
                var OK = false;
                if (tt[0] > ft[0]) {OK=true}
                if ((tt[0] == ft[0]) && (tt[1] > ft[1])) {OK = true}
                if (OK) {
                alert ("The second time is after the first time");
                var hr = tt[0] - ft[0];
                var mn = tt[1] - ft[1];
                if (mn < 0) {mn= mn+60; hr = hr-1}
                if (mn <10) {mn = "0" + mn}
                alert ("Difference is " + hr + ":" + mn);
                }
                else {alert ("The second time is NOT after the first time")}
                
                </script>

                Comment

                • narinas
                  New Member
                  • Jan 2012
                  • 20

                  #9
                  how to set this variables to indexes in grid and compare b/w those two cells.

                  Comment

                  • C CSR
                    New Member
                    • Jan 2012
                    • 144

                    #10
                    Your code seems to work okay. I don't think you need the "split" part in there, but it does work. The Results should be the same because as strings "05:50" is less that "6:21". And "05:50" is less than "05"55". But the way you did it gave you the datatype "number" to use for things like "the difference." Good Job!

                    I can't see the calling function for the code you sent at the top. What does that look like? Maybe we can create a similar function, but bring in both values and run your test, and then loop them through the code you already have, one at a time. Then return.

                    Maybe nest the old code inside your new code as a function. Not sure without see original call to "function validate_grid(v alue,id,ind)".

                    Comment

                    • C CSR
                      New Member
                      • Jan 2012
                      • 144

                      #11
                      I'm starting to write poorly. Taking a break for a while! Hang in there.

                      Comment

                      • narinas
                        New Member
                        • Jan 2012
                        • 20

                        #12
                        as im new to grid i dont know how to get the value of the cells i.e.indexhttp://www.dhtmlx.com/docs/products/...h_message.html
                        so that i can use the value in the variables

                        Comment

                        • narinas
                          New Member
                          • Jan 2012
                          • 20

                          #13
                          thank you for helping me.

                          Comment

                          • C CSR
                            New Member
                            • Jan 2012
                            • 144

                            #14
                            Do you have access to the "calling" procedure? I mean, something is calling the function you showed me at the top of this thread, not the one you wrote. What is calling "function validate_grid(v alue,id,ind)"?? ?

                            Comment

                            • narinas
                              New Member
                              • Jan 2012
                              • 20

                              #15
                              i have sent u an link about that function its used for validation part.& please find the attachment of my program.
                              Attached Files

                              Comment

                              Working...