i have this code, it's purpose is to automatically add a colon when the index is 2,
i have done it, but when replacing(overwrite) index 2 where (:) is, you can replace it with a different char, so basically colon will be removed.
and also what if the user has entered a single digit, what do you suggest about that?

i also need to add their values to get the total time, i'm still working on that but if you have any idea, that would be of big help, thanx

<html>
<FORM>
<table border="1">
<tr><th>Time</th>
<td><input type="text" name="alpha" value="" onkeypress="addColon(this.form.alpha, this.form.total);" maxlength="5"/></td>
<td><input type="text" name="beta" value="" onkeypress="addColon(this.form.beta, this.form.total);" maxlength="5"/></td>
<td><input type="text" name="gamma" value="" onkeypress="addColon(this.form.gamma, this.form.total);"  maxlength="5"/></td>
<td><input type="text" name="total" value="" disabled="disable"/></td>
</tr>

</table>
<input type="text" name="displaycount" size="20">
</FORM>

<SCRIPT TYPE="text/javascript">
    function addColon(what, outTo){
    var string = what.value;
    var i = string.indexOf(":");
    //alert(what.value.length);
        if(what.value.length ==2 && i < 0){
            what.value = what.value + ':';
        }
        //add condition 
        //alert(what.value);
        outTo.value = what.value
    }   

    function addColumns(){}
</SCRIPT>

</html>

`

..

commented: why all the extra ".."? +0

..

..

..

try this code. hope it help. =)

<SCRIPT TYPE="text/javascript">
    function addColon(what, outTo){
    //alert(what+' '+outTo);
    var string = what.value;    
    var i = string.indexOf(":");

    //alert(what.value.length);
        if(what.value.length ==2 && i < 0){
            what.value = what.value + ':';
        }
        if(what.value.length ==1 && what.value !="1"){
            what.value = "0"+what.value + ':';
        }
        //add condition 
        //alert(what.value);
        outTo.value = what.value
    }   

    function addC(obj){
    //alert(obj.length);
    }
</SCRIPT>

thanks for the reply, i've already fixed this problem, and i think i'll just use onchange on the input tag attribute ..
as for the computation i just split the first 2 digits from the 2nd 2 digits to determine the hours and minutes.
i have a quick question though, how do you delete the input box value using javascript ? is it possible?

anyway thanks .. i've solved it ..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.