JavaScript and VBScript function calling

- 1 answer

Ad

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...

Ad

Answer

Ad

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).

Ad
source: stackoverflow.com
Ad