...

View Full Version : Adding and subtracting, how to keep leading zero?



rtown
06-26-2012, 07:01 PM
Hey everyone,

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



<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.
:D

Philip M
06-26-2012, 07:36 PM
Try this:-


<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

WolfShade
06-26-2012, 07:38 PM
As far as padding zeroes:


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.)

rtown
06-26-2012, 09:53 PM
Try this:-


<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. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum