Need help! I need to add lead zeros to a textbox

Discussion in 'ASP .Net' started by Teep, Jun 21, 2004.

  1. Teep

    Teep Guest

    I have a simple text box called txtrefnum, if the user enters a number
    length less than 9 characters long than I need to have lead zeros
    added to it. Does anyone know how to do this? I couldn't find anything
    online on the subject...

    <asp:TextBox id="txtRefNum" runat="server"></asp:TextBox>

    I thought I could use a validator for the process and make the user
    add the zeros, but of course that is a big no-no! Please help!!

    Thanks!
    Teep, Jun 21, 2004
    #1
    1. Advertising

  2. Do you want to do it on clien side or server side ?

    on server side it is simple :

    If txtRefNum.Text.Length < 9 Then
    Dim zeros as String = ""
    Dim index as Integer = 0
    For index = txtRefNum.Text.Length To 8
    zeros &= "0"
    Next
    txtRefNum.Text = zeros & txtRefNum.Text
    End If

    Regards
    Martin

    "Teep" <> wrote in message
    news:...
    > I have a simple text box called txtrefnum, if the user enters a number
    > length less than 9 characters long than I need to have lead zeros
    > added to it. Does anyone know how to do this? I couldn't find anything
    > online on the subject...
    >
    > <asp:TextBox id="txtRefNum" runat="server"></asp:TextBox>
    >
    > I thought I could use a validator for the process and make the user
    > add the zeros, but of course that is a big no-no! Please help!!
    >
    > Thanks!
    Martin Marinov, Jun 21, 2004
    #2
    1. Advertising

  3. No need to write your own Pad() method, there's a String.PadLeft()
    method built into .NET. Check here for examples:

    http://msdn.microsoft.com/library/d...f/html/frlrfsystemstringclasspadlefttopic.asp

    That article demonstrates an easier / cleaner way to accomplish this
    task. An example:

    <snip>
    string temp = txtRefNum.Text.PadLeft(9, '0');
    </snip>

    This should be what you're looking for, Teep.


    Martin Marinov wrote:

    > Do you want to do it on clien side or server side ?
    >
    > on server side it is simple :
    >
    > If txtRefNum.Text.Length < 9 Then
    > Dim zeros as String = ""
    > Dim index as Integer = 0
    > For index = txtRefNum.Text.Length To 8
    > zeros &= "0"
    > Next
    > txtRefNum.Text = zeros & txtRefNum.Text
    > End If
    >
    > Regards
    > Martin
    >
    > "Teep" <> wrote in message
    > news:...
    >
    >>I have a simple text box called txtrefnum, if the user enters a number
    >>length less than 9 characters long than I need to have lead zeros
    >>added to it. Does anyone know how to do this? I couldn't find anything
    >>online on the subject...
    >>
    >><asp:TextBox id="txtRefNum" runat="server"></asp:TextBox>
    >>
    >>I thought I could use a validator for the process and make the user
    >>add the zeros, but of course that is a big no-no! Please help!!
    >>
    >>Thanks!

    >
    >
    >
    Larry Marburger, Jun 21, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    6
    Views:
    2,466
    =?Utf-8?B?SEFSSSBQUkFTRCBCQVJV?=
    Jun 21, 2004
  2. =?Utf-8?B?QXNoYQ==?=

    how add zeros to numbers...

    =?Utf-8?B?QXNoYQ==?=, Jan 18, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    405
    Kevin Spencer
    Jan 19, 2005
  3. Replies:
    0
    Views:
    430
  4. Ann
    Replies:
    0
    Views:
    393
  5. Ann
    Replies:
    1
    Views:
    187
    Anthony Levensalor
    Jan 2, 2008
Loading...

Share This Page