0

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>

`

2
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by azareth
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>
0

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?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.