By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

adding leading zeroes

P: 6
I need to add leading zeroes to an input box.
For example, the user enters "2" , I need to convert to a three digit number eg."002". If the user enters "23", I need to convert to "023"
If the user enters a three digit number leave it alone.

Thanks for your help.
Nov 20 '07 #1
Share this Question
Share on Google+
9 Replies


Death Slaught
100+
P: 1,137
By convert do you meen convert it in your variable, or convert it when the user enters it?

Thanks, Death
Nov 20 '07 #2

P: 6
By convert do you meen convert it in your variable, or convert it when the user enters it?

Thanks, Death
when the user enters it,
Nov 20 '07 #3

Death Slaught
100+
P: 1,137
You would have to check the users input, and then based on that input, change the value of text. How you would do this i'm not entirely sure. I know bits and pieces of what you would need to do, but no clue on a few parts.

Acoder or Gits will probably know, they should be along shortly.

Thanks, Death
Nov 20 '07 #4

P: 6
You would have to check the users input, and then based on that input, change the value of text. How you would do this i'm not entirely sure. I know bits and pieces of what you would need to do, but no clue on a few parts.

Acoder or Gits will probably know, they should be along shortly.

Thanks, Death
Thanks for your response. :-)
Nov 20 '07 #5

gits
Expert Mod 5K+
P: 5,320
hi ...

you may use something like the following example:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function fill_value(value) {
  3.     var l = 3;
  4.     var t = [];
  5.     var v = [];
  6.  
  7.     if (value.length <= l) {
  8.         t = value.split('');
  9.  
  10.         var tl = l - t.length;
  11.  
  12.         for (var i = 0; i < l; i++) {
  13.            if (i < tl) {
  14.                v.push('0');
  15.            } else {
  16.                for (var j = 0; j < t.length; j++) {
  17.                    v.push(t[j]);
  18.                }
  19.                break;
  20.            }
  21.         }
  22.     } else {
  23.         var re = new RegExp('^.{' + l + '}');
  24.  
  25.         v = value.replace(/.+/, value.match(re)).split('');
  26.     }
  27.     return v.join('');
  28. }
  29. </script>
  30. <body>
  31. <input type="text" name="test" value="000" 
  32.     onblur="this.value = fill_value(this.value);" onclick="this.select();"/>
  33. </body>
  34.  
kind regards
Nov 20 '07 #6

100+
P: 428
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
  5. <title>Untitled Document</title>
  6.  
  7. <script type="text/javascript">
  8.  
  9. function padZero(who,len){
  10.     var n= who.value;
  11.     n=n.replace(/^0+/,'');    
  12.     while(n.length< len) n= '0'.concat(n);
  13.     who.value= n;
  14. }
  15. </script>
  16. </head>
  17.  
  18. <body>
  19. <input type="text" name="digits" value="" style="text-align:right; font-weight:bold"  onkeyup="padZero(this,3)">
  20. </body>
  21. </html>
Nov 21 '07 #7

gits
Expert Mod 5K+
P: 5,320
heya mrhoo ...

nice :) much better then mine ... was late last evening ;)

kind regards
Nov 21 '07 #8

acoder
Expert Mod 15k+
P: 16,027
Acoder or Gits will probably know, they should be along shortly.
...or mrhoo, don't forget mrhoo! Nice work! :)
Nov 21 '07 #9

Death Slaught
100+
P: 1,137
[HTML]<html>

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

function add() {

document.doWhatISay="Add mrhoo";

}

</script>
</head>

<body>

<input type="button" onClick="add()" value="Add mrhoo" />

</body>

</html>[/HTML]

All fixed lol.

- Death

PS - Incase you don't already know this, the code above will not work.
Nov 21 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.