We help IT Professionals succeed at work.

Place leading zeros before a number.  VB.NET/ASP.NET

6,885 Views
Last Modified: 2010-05-18
VB.NET, ASP.NET .NET 2.0

I have a textbox on the screen that accepts user input.  This textbox accepts a maximum of a 9 digit number.  The format is four digit year followed by any 5 numbers.

For example: 200783244

The user wants me to place leading zeros so that the number is always 9 digits long.  So If the user enters: 200788.  After the user tabs out, I need to change the number to: 200700088

The leading zeros need to come after the year and before the number.

I'd preffer this to be client side script, but if that's to hard I'll just do it server side.  Like after the user hits submit I'll just format it before it goes to the database.
Comment
Watch Question

Are you already using Javascript?  If not, please use something like the following for server side:

Onclick event of submit button:
dim x as long
dim spacer as string
x = textbox1.text.length
if x <> 9 then
for y = 0 to 9 - textbox1.text.length
spacer = spacer & "0"
next
end if
textbox1.text = spacer & textbox1.text

This should give the desired results.  Let me know what you find.

Forgot to dim the y as long, also...
divinewind80:

That won't put the zeros after the year, as the poster requested.  That will always put them at the beginning of the string.

Try this:

TextBox1.Text = TextBox1.Text.Substring(0, 4) & TextBox1.Text.Substring(4).PadLeft(5, "0")
Here is a javascript solution for you.  Of course, just place this function in the HEAD inside some SCRIPT tags:

function padWithZeros(string) {
        var pad = '000000000'+ string;
        return pad.substring(pad.length - 9,pad.length);}

Then call the function like this:

<input type="text" onchange="this.value=padWithZeros(this.value);" />
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Fair point about server-side vs client-side.
Epicmanagement,

I thought you wanted a client-side solution.  Did you change your mind?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.