[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Error in script of adding value to integer

Posted on 2005-05-13
7
Medium Priority
?
253 Views
Last Modified: 2010-04-17
I'm probably missing something very easy..  The value of the text box is not to exceed 59 and once it reachers 59 the next value shall be 00.  The problem I'm having is after the value hits 08 it then goes to 01.

The test code is below to help you. thanks


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script>
  function add_time (obj) {                
      var val=0;
      var ret=0;
    val = (parseInt(obj.value));
      if(val == 59) {
        ret = 0;
      }else{      
          ret = (parseInt(val) + 1);
      }       
      if(parseInt(ret) < 10) {
        ret = ret +"";
        obj.value = "0"+ret;
      }else{
        obj.value = ret;      
      }      
  }
 
  function subtract_time () {
  }  
 
</script>

</head>

<body>
<form>
<table>
  <tr>
    <td><input type="text" value="00" id="minutes" name="minutes"></td>
    <td>
        <input type="button" value="+" onClick="add_time(document.getElementById('minutes'))">
      <input type="button" value="-">              
      </td>      
  </tr>
</table>
</form>
</body>
</html>
0
Comment
Question by:dgelinas
6 Comments
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13998134
Hmm.. I have no idea why your other code doesn't work (I'm getting the same problem as you), but the following works fine for me:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>

<script language="javascript" type="text/javascript">

  function add_time (obj) {              
     obj.value++;
     if ( obj.value > 59 )
       obj.value = 0;
     
     if ( obj.value < 10 )
       obj.value = "0" + obj.value;  
  }
 
  function subtract_time () {
  }  
 
</script>

</head>

<body>
<form>
<table>
  <tr>
    <td><input type="text" value="00" id="minutes" name="minutes"></td>
    <td>
       <input type="button" value="+" onClick="add_time(document.getElementById('minutes'))">
      <input type="button" value="-">            
     </td>    
  </tr>
</table>
</form>
</body>
</html>

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 13998368
The reason that it jumps back to 01 after 08 is because the parseInt() function sees the 08 as an octal number (base 8), and since 08 is an invalid octal number, it returns 0.

If you still want to use the parseInt function, just change all usages of this function from :

parseInt(val)

to

parseInt(val, 10)

specifying that we want the number to be read in base 10, or as a decimal number.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 13998938
Ah! Nice one Infinity08  :-)   lol, your username is a little ironic (08(  ^_^
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 53

Expert Comment

by:Infinity08
ID: 14000967
:) It is, isn't it ? :) Although the 8 should be viewed on its side ... :)
0
 

Expert Comment

by:ganyew
ID: 14007765
//Please try below function

function add_time (obj) {              
     var val=0;
     var ret=0;

     if (obj.value.substring(0,1) == "0")
      ret = parseInt(obj.value.substring(1));
     else
      ret = parseInt(obj.value);

     ++ret;
      if (ret > 59)
       ret = 0; //So that 59 still will display, but if reach 60 the value will change to "00".

      if (ret < 10)
       obj.value = "0" + ret;
      else
        obj.value = ret;    
  }
0
 

Accepted Solution

by:
OzzMod earned 0 total points
ID: 14059412
Closed, no points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Loops Section Overview

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question