How to TRIM zeroes from the left portion of a string

Posted on 2008-11-10
Last Modified: 2012-05-05
Classic ASP VBScript


I have a login page for our team where a user enters their employee number.  Sometimes, however, they enter their "full" employee number, which just contains 3 leading zeroes ("000.....") before their number.

This, of course, causes problems when logging in because the entered datat does not contain the leading zeroes.

I would like for any zeroes to the left of a number to be ignored, or removed, from the login after the user presses "submit".  I was looking at TRIM, LTRIM, RTRIM, but they appear to just remove spaces.

How could I accomplish removing/ignoring zeroes?

Example emp number:  203456
Example login (Sometimes): 000203456

Basically, I would like any number of zeroes (Whether 2 or 5, etc.) to be removed/ignored from the left side until it reaches any number other than zero.

Thanks so much,
Question by:lshane
    LVL 82

    Expert Comment

    Dim x
    x = CInt(x)

    Author Comment

    Thanks, hielo, for your reply.  That's kind of what I need.  More specifically, though, I need to remove any number of zeroes in the front before a "different" number appears.

    Sometimes a user could enter "00123456"...
    Other times they could enter "000123456"...
    Or even "0123456"...

    I just want to be able to have it ignore/remove any zero (however many) in the beginning of the string UNTIL it hits the first "different" number (1, 2, 3, 4, etc.).

    Is that possible?
    LVL 82

    Accepted Solution

    That's effectively what CInt() will do. Did you try it? If you are having problems then add zero the result of CInt():

    Dim x
    x = 0 + CInt(x)

    x should now be just 123

    Author Comment

    Hello, hielo.

    OK - yes, it did work - to a certain degree.

    However... if I type "000123456" or even "123456" then I get the following error:
    Error Type:
    Microsoft VBScript runtime (0x800A0006)
    Overflow: 'CInt'
    /testpmcct2/testloginresults.asp, line 5

    Browser Type:
    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 2.0.50727)

    POST 42 bytes to /testpmcct2/testloginresults.asp

    POST Data:

    It's almost like it gives the error if I type MORE than 5 numbers other than zero.
    True:  It does remove any zeroes from the beginning, however, it only accepts a 5-digit number after that.

    How can that be extended?

    Author Comment

    Hi, hielo.

    I discovered if I remove the "C" from the "Cint", that it is able to extend the numbers.

    So, what is the significance of the "C" in "Cint" ?

    LVL 82

    Expert Comment

    CInt: Converts an expression to a variant of subtype Integer
    Int:        Returns the integer part of a specified number

    CLng would have also given you a bigger range

    Author Closing Comment

    Awesome!  Thanks so much, hielo!  I appreciate it.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:  The Exchange of information …
    I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now