0

HTML Form:

<form name="CheckIn" method="post" action="check.asp" onsubmit="return CheckBreakTime() && CheckTime();" > 

I call two functions whit onsubmit,the functions return true or false,but function works only if it's "first" ,for example if i say ' onsubmit="return CheckTime() && CheckBreakTime();" ' only CheckTime works if i call ChecKBreakTime first it only works.

The Functions (JavaScript) :

function CheckBreakTime(){ if (document.getElementById('breakco').checked) { var BKTimeDif1 = '<%=BTimeDif%>'; var var1 = 20 ; var sum1 = var1 - BKTimeDif1 ; if (BKTimeDif1 > 10 && BKTimeDif1 < 21) { alert("You were on a break longer than 10 minutes,You must wait " + sum1 + " minutes to pass to check out from break. "); return false; } else { return true; } } else { return true; } } function CheckTime() { if (document.getElementById('breakci').checked) { var TimeDif2 = '<%=BTimeDiff%>'; var TimeDif1 = '<%=TimeDif%>'; if (TimeDif1 < 120) { alert("You must work at least two hours before you can go on a break."); return false; } else { if (TimeDif2 != 0 && TimeDif2 < 120) { alert("You must work at least two hours before you can go on another break."); return false; } else { return true; } } } else { return true; } } 

and the VBScript code that i put in JavaScript:

Dim TimeDif TimeDif=Round(Csng(DateDiff("n", (HourList.Fields.Item("checkInTime").Value), (Now()))), 2) Dim BTimeDif If Not IsNull(HourList.Fields.Item("breakCheckIn").Value) Then BTimeDif = Round(Csng(DateDiff("n", (HourList.Fields.Item("breakCheckIn").Value), (Now()))), 2) End If If Not IsNull(HourList.Fields.Item("breakCheckOut").Value) Then Dim BTimeDiff BTimeDiff = Round(Csng(DateDiff("n", (HourList.Fields.Item("breakCheckOut").Value), (Now()))), 2) End If 

VBScript code works fine,it returns what it need to and JavaScript gets it.Can some tell me what is the problem...

3
  • If your server side vbs is correctly populating the values of TimeDif and BTimeDif then can I suggest that this is a javascript question and it doesn't need the other two tags? Commented Dec 24, 2015 at 15:15
  • 1
    You are comparing numbers to strings in CheckTime. Commented Dec 24, 2015 at 17:01
  • The problem was that i check buttons that are not displayed because of vbscript,so the javascript just check the first one.The answer is to put hidden inputs whit same id. Sorry if i offended,sorry for my bad grammar. Commented Mar 22, 2016 at 0:24

2 Answers 2

0

If you want both functions to run even if the first one evaluates to false - then you can use "&": onsubmit multiple javascript functions

But, personally I'd wrap them both in a function that aggregates the logic of both:

function checkBothTimes(){ if (!CheckBreakTime()){ return false; } else if (!CheckTime()){ return false; } return true; } 

Even though this is longer - it makes the code easier to read (to me).

Sign up to request clarification or add additional context in comments.

Comments

0

JavaScript uses short-circuit evaluation for its logical operators. That means that in an expression like expr1 && expr2, expr2 is evaluated only if expr1 evaluates to true. If expr1 evaluates to false there is no need to evaluate expr2, since the result of expr1 && expr2 will be to false no matter what.

So, if you run for example

var result = foo() && bar(); function foo(){ console.log("running foo!"); return false; } function bar(){ console.log("running bar!"); return false; } 

Only function foo() will be executed. This is effectively the same as:

var result = foo(); if (result){ result = bar(); } 

That's why only the first function in your expression gets called. If it returns false, there's no need to run the second one.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.