Solved

add leading zeroes

Posted on 2007-11-20
5
967 Views
Last Modified: 2012-06-21
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 a three digit number leave it alone.

thanks for your help .
0
Comment
Question by:marcelo27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 500 total points
ID: 20322895
$num = "2";
str_pad($num, 2, "0",STR_PAD_LEFT);

hope this helps.
0
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 500 total points
ID: 20322906
sorry i didn't see you wanted a javascript solution, please ignore my previous post.
0
 
LVL 21

Accepted Solution

by:
nizsmo earned 500 total points
ID: 20322947
however this should help:

<script ....>
var num = "2";
var zero = "0";
if(num.length==1)
{
  result = zero + zero + num;
}
else if(num.length==2)
{
  result = zero + num;
}
</script>
0
 

Author Comment

by:marcelo27
ID: 20323147
Im not sure why var num = "2"?
The user can enter any numerical value. If the user enters a one or double digit number zeroes need to be added. The maximum length of string is 3.
0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20323310
marcelo27:

i was giving an example and num is what you have to assign, my primary objective is to let you know that my function works :) if i didn't put the num there and an example value, you wouldn't be able to see the working code and it would be harder to understand.

hope that explains why i put "2" as num, you can put and assign any number from anywhere to the num variable.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Slush on text 2 26
Form submit takes only for one form 23 52
Javascript Error 10 24
Run or cancel ColdFusion code from running with a button press 2 3
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

734 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