Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    99
    Thanks
    10
    Thanked 1 Time in 1 Post

    Adding and subtracting, how to keep leading zero?

    Hey everyone,

    Need a hand with a little JS. The script below that adds and subtracts by 1, to a value already given.

    Code:
    <input type="text" name="TransNumber" id="TransNumber" size="5" value="0001" readonly="readonly" class="readonly">
    <input type="button" value="+" style="width:23px" onclick="Number(document.getElementById('TransNumber').value++)" >
    <input type="button" value="-" style="width:23px" onclick="Number(document.getElementById('TransNumber').value--)" >
    The existing value has leading zeros such as 0001. When you add or subtract the leading zeros disappear. I tried to tie a zero padding function into the onclick but this does not work.

    I would also like to disallow subtracting past 0001. So, 0000 and negative number should not be allowed.

    Any advice appreciated! Thanks.
    Last edited by rtown; 06-26-2012 at 08:05 PM. Reason: forgot the script...

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Try this:-

    Code:
    <input type="text" name="TransNumber" id="TransNumber" size="5" value="0001" readonly="readonly" class="readonly">
    <input type="button" value="+" style="width:23px" onclick="abc('+')" >
    <input type="button" value="-" style="width:23px" onclick="abc('-')" >
    
    <script type = "text/javascript">
    
    function abc(plusminus) {
    var digits = document.getElementById("TransNumber").value;
    digits = Number(digits);
    if ((plusminus == "-") && (digits <= 1)) {
    alert ("Zero or negative numbers are not allowed");
    return false;
    }
    if (plusminus == "+") {
    digits ++;
    }
    else {
    digits --;
    }
    
    var padded = String((digits*1) + 10000).slice(1); 
    document.getElementById("transNumber").value = padded;
    }
    
    </script>


    "This is the worst disaster in California since I was elected." - California Governor Pat Brown, discussing a local flood
    Last edited by Philip M; 06-26-2012 at 08:58 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    rtown (06-26-2012)

  • #3
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    As far as padding zeroes:
    Code:
    String.prototype.pad = function(l,s){
      return(l -= this.length) > 0
        ? (s = new Array(Math.ceil(l / s.length) + 1).join(s)).substr(0,s.length) + this + s.substr(0,l-s.length)
        : this ;
    };
    var x = Math.floor(equation).toFixed().pad(5,'0'); will pad the integer for a total of five characters (ie, 1 = 00001, 25 = 00025, etc.)
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #4
    New Coder
    Join Date
    Oct 2010
    Posts
    99
    Thanks
    10
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    Try this:-

    Code:
    <input type="text" name="TransNumber" id="TransNumber" size="5" value="0001" readonly="readonly" class="readonly">
    <input type="button" value="+" style="width:23px" onclick="abc('+')" >
    <input type="button" value="-" style="width:23px" onclick="abc('-')" >
    
    <script type = "text/javascript">
    
    function abc(plusminus) {
    var digits = document.getElementById("TransNumber").value;
    digits = Number(digits);
    if ((plusminus == "-") && (digits <= 1)) {
    alert ("Zero or negative numbers are not allowed");
    return false;
    }
    if (plusminus == "+") {
    digits ++;
    }
    else {
    digits --;
    }
    
    var padded = String((digits*1) + 10000).slice(1); 
    document.getElementById("transNumber").value = padded;
    }
    
    </script>
    This worked perfectly. Thank you sir! I did not know how to build a function into an onclick.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •