Jan 18th, 2010, 11:40 AM
Hey bvdveen,
No worries! It's always a bit rocky when you start programming.... but then you get hooked.
I'd tackle the problem slightly differently, but your solution would work too. Take a look at the following code (I confess I haven't tested it, but the logic is sound):
Let me know how it goes!
- Ben
No worries! It's always a bit rocky when you start programming.... but then you get hooked.
I'd tackle the problem slightly differently, but your solution would work too. Take a look at the following code (I confess I haven't tested it, but the logic is sound):
Code:
function check_times()
{
// get the values in the two dropdowns (you'll need to replace
// "formnamehere" with the name attribute of your form)
var time1 = document.formnamehere.BW.value;
var time2 = document.formnamehere.VZ.value;
// if either are empty, just do nothing. If the fields ARE required,
// just add another RSV "required" rule BEFORE calling this custom function
if (!time1 || !time2)
{
return true;
}
// split the info in each time on the ":" character
var time1_info = time1.split(":");
var time1_hours = parseInt(time1_info[0]); // parseInt sucks, but it's OK here
var time1_mins = parseInt(time1_info[1]);
var time2_info = time2.split(":");
var time2_hours = parseInt(time2_info[0]);
var time2_mins = parseInt(time2_info[1]);
// the error message is always going to be the same, so let's just stash it
// in a variable here so we don't have to type it out multiple times.
var errorInfo = [document.formnamehere.VZ, "Sorry, please enter a later time."];
// alright! Now we do our validation. First check the hours: if the hour in time2
// is less than time1, Houston we have a problem
if (time2_hours < time1_hours)
{
return [errorInfo];
}
else if (time2_hours == time1_hours)
{
// here, the user entered the same hour for both. So,check that the
// mins in time2 isn't greater of equal to that in time1
if (time2_mins <= time1_mins)
{
return [errorInfo];
}
}
// we're good!
return true;
}
Let me know how it goes!
- Ben