Welcome to Dream.In.Code
Become an Expert!

Join 149,812 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,758 people online right now. Registration is fast and FREE... Join Now!




Keeping parseInt from assuming Octal

 
Reply to this topicStart new topic

Keeping parseInt from assuming Octal, Leading zeroes make parseInt assume octal values

wzeller
3 Feb, 2007 - 10:23 PM
Post #1

New D.I.C Head
Group Icon

Joined: 9 Apr, 2006
Posts: 39


Dream Kudos: 25
My Contributions
Hello All,

In a lengthy function that I have for helping make data entry into some numeric fields better, I have this little gem:

CODE
document.fulfill.sku1.value = parseInt(document.fulfill.sku1.value);


This is part of a function that is fired every time the the value of sku1 changes. This particular command does neat stuff for me like trimming leading or trailing whitespace and trimming any non-numeric characters that come after numbers.

The problem is leading zeroes. sku1 is just one of several fields, all of which have 0 as a default value. If the user clicks next the zero and enters a value like "034", they expect that to be the same as "34". Instead, it instantly changes to "28". Trying to explain to the average user that leading zeroes indicate to Javascript that they are using a different base counting system doesn't seem to go over too well. I get this look: blink.gif

Is there a way to tell parseInt to assume decimal input?

Wayne

This post has been edited by wzeller: 3 Feb, 2007 - 10:24 PM
User is offlineProfile CardPM
+Quote Post

wzeller
RE: Keeping ParseInt From Assuming Octal
5 Feb, 2007 - 12:34 PM
Post #2

New D.I.C Head
Group Icon

Joined: 9 Apr, 2006
Posts: 39


Dream Kudos: 25
My Contributions
Well,

I came up with a kludgy workaround:

CODE
onBlur="if (this.value==''){this.value='0';}" onFocus="if (this.value=='0'){this.value='';}"


Adding that to the fields makes the default 0 disappear so that users don't type their numbers after the zero already in the field. If they tab or click out without giving a number then the 0 comes back, and if they put a number in then it is left alone.

There is still the issue of people typing a leading zero, but at that point I don't feel too sorry for them.

If anybody knows of a proper solution to the issue, I'd still love to hear it - that way I could even fix it for typed leading zeroes. But the priority has gone way down for me.

Wayne

This post has been edited by wzeller: 5 Feb, 2007 - 12:35 PM
User is offlineProfile CardPM
+Quote Post

Arbitrator
RE: Keeping ParseInt From Assuming Octal
6 Feb, 2007 - 02:56 PM
Post #3

D.I.C Regular
Group Icon

Joined: 26 Jan, 2005
Posts: 492



Thanked: 2 times
My Contributions
QUOTE(wzeller @ 4 Feb, 2007 - 12:23 AM) *
Is there a way to tell parseInt to assume decimal input?
I don’t know if there’s a way to do that, but you can strip the leading zeroes as an alternative (live examples). Note that the examples use standards‐compliant JavaScript (and CSS) and thus aren’t supported by Internet Explorer; you’ll have to adjust the code to get them to work in that browser.

CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en-US">
  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="Author" content="Patrick Garies">
    <title>DIC 23550 Script: Kill Leading Zeroes in Form Fields</title>

    <style type="text/css">
      * { margin: 0; font: 18px/1.4 "Lucida Sans Unicode", "Lucida Grande", sans-serif; }
      body { display: table; margin: 0 auto; border-spacing: 1em; text-align: center; }
      body > div { display: table-row; }
      h1 { margin: 0.1em 0; font-weight: bold; text-align: left; }
      label::after { content: " "; }
      span::after { content: ":"; }
      code { vertical-align: top; font-family: "Courier New", monospace; }
      input { width: 8em; }
      div.message { color: green; }
    </style>

    <script type="text/javascript">
      var input;

      document.defaultView.addEventListener("load", basic, false);

      function basic() {
        input = document.getElementsByTagName("input");
        input[0].addEventListener("keyup", validate, false);
        input[0].addEventListener("focus", clear, false);
        input[0].addEventListener("blur", restore, false);
        input[2].addEventListener("focus", clear, false);
        input[2].addEventListener("blur", restore, false);
        }

      /* This prevents the input of leading zeroes. */
      function validate() {
        for (var i = 0; this.value.charAt(i) == "0" && this.value.length != 1; i++) {
          this.value = this.value.substring(1);
          }
        }

      /* This returns an integer with the leading zeroes stripped. */
      function calculate() {
        var i = 0;
        for (i; input[2].value.charAt(i) == "0"; i++) {
          if (parseInt(input[2].value) == 0) {
            input[2].value = "0"; /* For example, “00” is reduced to “0”. */
            break;
            }
          }
        var output = document.createElement("div");
        output.className = "message";
        output.appendChild(document.createTextNode("The number specified is " + parseInt(input[2].value.substring(i)) + "."));
        document.getElementsByTagName("body")[0].appendChild(output);
        }

      /* This clears the default value if the default value is specified. */
      function clear() {
        if (this.value == this.defaultValue) {
          this.value = "";
          }
        }

      /* This restores the default value if no value is specified. */
      function restore() {
        if (this.value == "") {
          this.value = this.defaultValue;
          }
        }
    </script>

  </head>
  <body>

    <div>
      <h1>Process Using <code>validate()</code></h1>
      <label><span>Number</span> <input type="text" value="0"></label>

      <input type="submit" value="Submit">
    </div>

    <div>
      <h1>Process Using <code>calculate()</code></h1>
      <label><span>Number</span> <input type="text" value="0"></label>
      <input type="submit" onclick="calculate();" value="Submit">

    </div>

  </body>
</html>


This post has been edited by Arbitrator: 6 Feb, 2007 - 02:58 PM
User is offlineProfile CardPM
+Quote Post

rooney
RE: Keeping ParseInt From Assuming Octal
12 Feb, 2007 - 07:11 AM
Post #4

New D.I.C Head
*

Joined: 12 Feb, 2007
Posts: 1


My Contributions
I am a newbie in JavaScript. I am trying to input values with 2 decimal points. e.g. 1234.56. When I type 1234.5 or 1234 everything is ok but when I input the whole number I am getting NaN. Here is the program:

<html>
<head>
<title>Examples 8.13 - Salesperson and Commissions</title>

<script = "JavaScript">

var salary = 200, item, itemValue, commission, total=0, grandTotal;


item = window.prompt( "Enter Item (-1 to Quit): ", "0" );

itemValue = parseInt ( item );

while ( item != -1) {
total += item;

item = window.prompt( "Enter Item (-1 to Quit): ", "0" );

itemValue = parseInt ( item );

}

commission = 9 / 100 * total;
grandTotal = salary + commission;

document.writeln( "<br />Total Pay: " + grandTotal );



</script>
</head><body></body>
</html>

Any help please?

QUOTE(Arbitrator @ 6 Feb, 2007 - 03:56 PM) *

QUOTE(wzeller @ 4 Feb, 2007 - 12:23 AM) *
Is there a way to tell parseInt to assume decimal input?
I don’t know if there’s a way to do that, but you can strip the leading zeroes as an alternative (live examples). Note that the examples use standards‐compliant JavaScript (and CSS) and thus aren’t supported by Internet Explorer; you’ll have to adjust the code to get them to work in that browser.

CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en-US">
  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="Author" content="Patrick Garies">
    <title>DIC 23550 Script: Kill Leading Zeroes in Form Fields</title>

    <style type="text/css">
      * { margin: 0; font: 18px/1.4 "Lucida Sans Unicode", "Lucida Grande", sans-serif; }
      body { display: table; margin: 0 auto; border-spacing: 1em; text-align: center; }
      body > div { display: table-row; }
      h1 { margin: 0.1em 0; font-weight: bold; text-align: left; }
      label::after { content: " "; }
      span::after { content: ":"; }
      code { vertical-align: top; font-family: "Courier New", monospace; }
      input { width: 8em; }
      div.message { color: green; }
    </style>

    <script type="text/javascript">
      var input;

      document.defaultView.addEventListener("load", basic, false);

      function basic() {
        input = document.getElementsByTagName("input");
        input[0].addEventListener("keyup", validate, false);
        input[0].addEventListener("focus", clear, false);
        input[0].addEventListener("blur", restore, false);
        input[2].addEventListener("focus", clear, false);
        input[2].addEventListener("blur", restore, false);
        }

      /* This prevents the input of leading zeroes. */
      function validate() {
        for (var i = 0; this.value.charAt(i) == "0" && this.value.length != 1; i++) {
          this.value = this.value.substring(1);
          }
        }

      /* This returns an integer with the leading zeroes stripped. */
      function calculate() {
        var i = 0;
        for (i; input[2].value.charAt(i) == "0"; i++) {
          if (parseInt(input[2].value) == 0) {
            input[2].value = "0"; /* For example, “00” is reduced to “0”. */
            break;
            }
          }
        var output = document.createElement("div");
        output.className = "message";
        output.appendChild(document.createTextNode("The number specified is " + parseInt(input[2].value.substring(i)) + "."));
        document.getElementsByTagName("body")[0].appendChild(output);
        }

      /* This clears the default value if the default value is specified. */
      function clear() {
        if (this.value == this.defaultValue) {
          this.value = "";
          }
        }

      /* This restores the default value if no value is specified. */
      function restore() {
        if (this.value == "") {
          this.value = this.defaultValue;
          }
        }
    </script>

  </head>
  <body>

    <div>
      <h1>Process Using <code>validate()</code></h1>
      <label><span>Number</span> <input type="text" value="0"></label>

      <input type="submit" value="Submit">
    </div>

    <div>
      <h1>Process Using <code>calculate()</code></h1>
      <label><span>Number</span> <input type="text" value="0"></label>
      <input type="submit" onclick="calculate();" value="Submit">

    </div>

  </body>
</html>



This post has been edited by rooney: 12 Feb, 2007 - 07:13 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 08:33AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month