add leading zeroes

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 .
Who is Participating?

Improve company productivity with a Business Account.Sign Up

nizsmoConnect With a Mentor DeveloperCommented:
however this should help:

<script ....>
var num = "2";
var zero = "0";
  result = zero + zero + num;
else if(num.length==2)
  result = zero + num;
nizsmoConnect With a Mentor DeveloperCommented:
$num = "2";
str_pad($num, 2, "0",STR_PAD_LEFT);

hope this helps.
nizsmoConnect With a Mentor DeveloperCommented:
sorry i didn't see you wanted a javascript solution, please ignore my previous post.
marcelo27Author Commented:
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.

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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.