JavaScript and VBScript function calling
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
Related Questions
Ad
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad