Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

But it really is false

Status
Not open for further replies.

Echilon

Programmer
Feb 22, 2007
54
GB
I have a validation function which compares two values, startsecs and endsecs. If startsecs is greater than endsecs, an error should be displayed, but it's not working because the if statement is evaluating as true, when it should be false.

You can see this by going to uncheck the box that says 'this event last all day', then enter 12:00 as the start time and 17:00 as the end time, and click save. Evevn in the error that's displayed, the second value is greater than the first, so it should evaluate as false.

The function can be seen as on line 223.
Code:
        var startsecs = (document.getElementById('starthour').value * 60) + document.getElementById('startminute').value;
        var endsecs = (document.getElementById('endhour').value * 60) + document.getElementById('endminute').value;
        if(startsecs > endsecs) {
            document.getElementById('error_end').innerHTML = 'End time must be after start time:'+endsecs+'::'+startsecs;
            document.getElementById('error_end').style.display = 'block';
            valid = false;
        }
 
Try using TypeOf just before the if statement for your values startsecs and endsecs.

document.write(typeof(startsecs))
document.write(typeof(endsecs))
document.end;

see what the type is I think that the types are not integers you might have to change it to an integer.
 
enter 12:00 as the [!]start[/!] time and 17:00 as the [!]end[/!] time, and click save

Code:
if(startsecs > endsecs) {

Seems to be working for me.
If you put 12 as the start and 17 as the stop, that should NOT pass an error (and it doesn't).



[small]"I see pretty girls everywhere I look, everywhere I look, everywhere I look. - Band song on movie "The Ringer"[/small]
<.
 
Problem solved, thanks for the help.

I needed to multiply the second .value by 1 to force it's type to integer.
 
Not to be a stickler, but that is a bad way to force a parse. You should really use the parseInt method.
Code:
        var startsecs = [!]parseInt([/!](document.getElementById('starthour').value * 60) + document.getElementById('startminute').value[!], 10)[/!];
        var endsecs = [!]parseInt([/!](document.getElementById('endhour').value * 60) + document.getElementById('endminute').value[!], 10)[/!];

[small]"I see pretty girls everywhere I look, everywhere I look, everywhere I look. - Band song on movie "The Ringer"[/small]
<.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top