I’m still new to Javascript. I have a web form that I use to insert a time (hh:mm) and I want a way to check whether time 1 is larger than time 2. I have:
function ValidateForm(){
var emailAddress=document.configChange.emailAddress
var timesmallH=document.configChange.timesmallH
var timesmallM=document.configChange.timesmallM
var timelargeH=document.configChange.timelargeH
var timelargeM=document.configChange.timelargeM
var timesmallSEC = ((timesmallH * 60 * 60) + (timesmallM * 60))
var timelargeSEC = ((timelargeH * 60 * 60) + (timelargeM * 60))
if ((emailAddress.value==null)||(emailAddress.value=="")){
alert("Please Enter an Email Address")
emailAddress.focus()
return false
}
if (timesmallSEC > timelargeSEC){
alert("warning - times are incorrect")
timesmallH.focus()
return false
}
return true
}
The email address part works fine, but the time section seems to be being ignored. I have checked the names are all correct, and logically it seems to make sense to me,but something is wrong.
e.g. If I put in
timesmallH = 0
timesmallM = 10
timelargeH = 0
timelargeM = 5
var timesmallH=document.configChange.timesmallH
var timesmallM=document.configChange.timesmallM
var timelargeH=document.configChange.timelargeH
var timelargeM=document.configChange.timelargeM
to
var timesmallH=document.configChange.timesmallH.selectedIndex
var timesmallM=document.configChange.timesmallM.selectedIndex
var timelargeH=document.configChange.timelargeH.selectedIndex
var timelargeM=document.configChange.timelargeM.selectedIndex
It now works, BUT, only if both options are changed, because it seems to be reading the position in the list, not the value. As I display the previous value by default (in position 0), if only one option gets changed it doesn’t work.
I need a way of doing the check against the value, not the position.
To extract the value from the selected entry in the first select list you would use:
var timesmallH =
document.configChange.timesmallH[document.configChange.timesmallH.selectedIndex];
document.configChange.timesmallH is an array containing all the values that the options in the select contain and so you need to get the selectedIndex entry from that array in order to get the value you want.