By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,626 Members | 1,653 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,626 IT Pros & Developers. It's quick & easy.

Validate time where start time should be less than end time

P: 20
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

Expand|Select|Wrap|Line Numbers
  1. var err_str="";
  2. function validate_grid(value,id,ind)
  3.     {
  4.     if(ind==1 || ind==2)
  5.         {
  6.             var patt=/^([0][0-9]|[1][0-9]|[2][0-3])[:]{1}[0-5][0-9]$/;
  7.             if(!patt.test(value))
  8.             {
  9.                 mygrid.setCellTextStyle(id,ind,"background-color:yellow;");
  10.                 if(err_str!="")
  11.                     err_str+="\n"+(ind+1)+".Enter numbers and : only..";
  12.                 else
  13.                     err_str=(ind+1)+".Enter numbers and : only";
  14.                 return false;
  15.             }
  16.             else
  17.             {
  18.                 mygrid.setCellTextStyle(id,ind,"background-color:white;");
  19.                 return true;
  20.             }
  21.         }
  22. }
  23.  
Jan 19 '12 #1
Share this Question
Share on Google+
16 Replies


100+
P: 144
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.
Jan 19 '12 #2

100+
P: 144
And I forgot: Are you using AM & PM with these values?
Jan 19 '12 #3

P: 20
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
Jan 19 '12 #4

100+
P: 144
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 "controllable" 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?
Jan 19 '12 #5

100+
P: 144
Correction: 22 is not less than 19, but you see what I mean :)
Jan 19 '12 #6

100+
P: 144
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?
Jan 19 '12 #7

P: 20
yes if i take 08:00 and 8:00 its showing error.look at this code
Expand|Select|Wrap|Line Numbers
  1. <script type = "text/javascript">
  2.  
  3. var fromTime = "05:50";  // format hh:mm
  4. var toTime = "06:21";
  5. var ft = fromTime.split(":");
  6. var tt = toTime.split(":");
  7. var OK = false;
  8. if (tt[0] > ft[0]) {OK=true}
  9. if ((tt[0] == ft[0]) && (tt[1] > ft[1])) {OK = true}
  10. if (OK) {
  11. alert ("The second time is after the first time");
  12. var hr = tt[0] - ft[0];
  13. var mn = tt[1] - ft[1];
  14. if (mn < 0) {mn= mn+60; hr = hr-1}
  15. if (mn <10) {mn = "0" + mn}
  16. alert ("Difference is " + hr + ":" + mn);
  17. }
  18. else {alert ("The second time is NOT after the first time")}
  19.  
  20. </script>
  21.  
Jan 19 '12 #8

P: 20
how to set this variables to indexes in grid and compare b/w those two cells.
Jan 19 '12 #9

100+
P: 144
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(value,id,ind)".
Jan 19 '12 #10

100+
P: 144
I'm starting to write poorly. Taking a break for a while! Hang in there.
Jan 19 '12 #11

P: 20
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
Jan 19 '12 #12

P: 20
thank you for helping me.
Jan 19 '12 #13

100+
P: 144
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(value,id,ind)"???
Jan 19 '12 #14

P: 20
i have sent u an link about that function its used for validation part.& please find the attachment of my program.
Attached Files
File Type: doc ta_canteen_rates_grid(main page).doc (52.0 KB, 533 views)
Jan 20 '12 #15

100+
P: 144
Narinas:

After looking around at the DHX site and the code piece you have, I have to say this is fairly big project altogether--lots of components. But sticking to what we've been discussing I made a few observations. First, the "value" appears to be any length of string, basically whatever you want to type in. And there's only one (1) value per set of coordinates (indexes). That means the entire string would have to be parsed just to determine where a time-string might be, if there was one, before comparing it in any way. In other words, the grid itself does not specifically have any "time controls" in it (like From: & To:); but I did notice "time-controls" on another page on a "scheduler" component, where outside of its grid there were startTime & endTime controls to set up a time block attached to a string value for "notes" in each location. But those controls, where you set the time block, have built-in validation and you wouldn't be responsible for coding that.

I know I'm talking about 2 different parts of a scalable project, and you're only working on 1 of these two (I think) based on the design sheet I got from you. The one you are working on does not have any time-controls (and I don't know exactly how you would insert them), so the user would be able to input anything they want with no restrictions, and as a "string" value the application doesn't care if there are numbers separated by colons in the middle of it (e.g., 00:00:00) and it doesn't keep track of where they might be inside its string. The cell (e.g., x1,y1), or let's just call it a "box," only allows a single Value at this point, and that's a problem if you want 2 distinct strings to compare.

Now, I saw in there where you can specify the Value's format for input, so you could set up a "mask" according to their formatting constants (or make some custom constant if its allowed) and at least get some control over the input (like xx:xx_xx:xx) and then parse that. But then thats all the grid were be good for.

So, I'm dissecting this as best I can, but its obviously daunting and a rather time consuming project. My advice is to get with the creators of this product and get some real good answers. The code is also a little beyond me with the PHP and its proprietary objects.

I hope some of this makes sense and forgive me where I might be confused--there are a lot of applets and libraries to study at the DHX site to build your application. You have to start there for the best information.

I'll watch this post to see how it goes. Thank you.
Jan 20 '12 #16

P: 20
Thanks a lot csr,im also totally confused by this grid structure.please help me out as soon as possible.thank you.
Jan 20 '12 #17

Post your reply

Sign in to post your reply or Sign up for a free account.