i have this code where it computes for the time entered by the user.
it doesn't have yet the limitations or the conditions regarding the time to be entered .
it successfully computes for the time and the hour increases based on the minutes, but the problem is when you entered "9" for the hour it doesn't add to the total but the rest is fine.. anyone can help ?
so here it is:
<html>
<FORM>
<table name="tblSamp" border="1">
<tr><th>Time</th>
<td><input type="text" name="alpha" value="" onchange="addColon(this.form.alpha, this.form.total);" maxlength="5"/></td>
<td><input type="text" name="beta" value="" onchange="addColon(this.form.beta, this.form.total);" maxlength="5" /></td>
<td><input type="text" name="gamma" value="" onchange="addColon(this.form.gamma, this.form.total);" maxlength="5"/></td>
<td><input type="text" name="total" value="" readonly="readonly" /></td>
</tr>
</table>
<input type="hidden" name="displaycount" size="20">
</FORM>
<SCRIPT TYPE="text/javascript">
var hours = new Array();
var mins = new Array();
function addColon(what, outTo){
var tHours = 0;
var tMins = 0;
var tTime = 0;
var adder = 0;
var min = 0;
var tempMins = 0;
var string = what.value;
var strlen = string.length;
var i = string.indexOf(":");
var tbl = document.getElementById("tblSamp");
var cIndex = what.parentNode.cellIndex;
//alert("cell index=" + cIndex);
//alert("index of (:): " + i);
//alert("string length " + strlen);
if(string != "" & !isNaN(string)){
if(i < 0){//if colon == 0
if(strlen < 3){//for input 12 = 12:00
//alert("for input 12 = 12:00");
hours[cIndex] = string;
mins[cIndex] = '00';
string = string + ':00';
what.value = string;
}
else if(strlen < 4){//for input 123 = 01:23
//alert("for input 123 = 01:23");
hours[cIndex] = '0' + string.charAt(0);
mins[cIndex] = string.charAt(1) + string.charAt(2);
string = hours[cIndex] + ':' + mins[cIndex];
//alert(string);
what.value = string;
}
else if(strlen < 5){//for input 1234 = 12:34
//alert("for input 1234 = 12:34");
hours[cIndex] = string.charAt(0) + string.charAt(1);
mins[cIndex] = string.charAt(2) + string.charAt(3);
string = hours[cIndex] + ':' + mins[cIndex];
//alert(string);
what.value = string;
}
else if(strlen > 4){//for input 12345 = Invalid Input
alert("Invalid Input!");
what.value = "";
}
}
else {
alert("Invalid Input!");
what.value = "";
}
}
else if(string != ""){
if(i > 0 && i < 3){
//alert("with :");
if(strlen < 5){//for input 1:23 = 01:23
//alert("for input 1:23 = 01:23");
hours[cIndex] = '0' + string.charAt(0);
mins[cIndex] = string.charAt(2) + string.charAt(3);
string = hours[cIndex] + ':' + mins[cIndex];
//alert(string);
what.value = string;
}
else {
//alert("correct input");
hours[cIndex] = string.charAt(0) + string.charAt(1);
mins[cIndex] = string.charAt(3) + string.charAt(4);
}
}
}
//end if
var lastCol = 4;
var table = document.getElementById('tblSamp');
for(var i=1;i<lastCol;i++){
if(mins[i] != "" && !isNaN(mins[i]) && hours[i] != "" && !isNaN(hours[i])){
tMins = eval(tMins) + parseInt(mins[i]);
//alert("total mins: "+tMins);
if(tMins > 59){
tempMins = tMins;
min = tempMins % 60;
tMins = tMins - min;
adder = tMins/60;
tHours += adder;
tMins = min
}
tHours = eval(tHours) + parseInt(hours[i]);
//alert("total hours: "+tHours);
}
}
tTime = tHours + ':' + tMins;
//alert("TOTAL=" + tTime);
outTo.value = tTime;
}
</SCRIPT>
</html>
just copy the whole thing and you can try it ...
i really need help in this. i can't see where did that problem come from ..
cheers,