Help! Need to add lead zeros to a textbox input

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

  1. TN Bella

    TN Bella 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!




    *** Sent via Devdex http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
    TN Bella, Jun 21, 2004
    #1
    1. Advertising

  2. Hi sorry I add my question hehe I cant write new queston it say the session is expired do you have any idea?

    "TN Bella" wrote:

    > 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!
    >
    >
    >
    >
    > *** Sent via Devdex http://www.devdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    >
    =?Utf-8?B?bWFoc2E=?=, Jun 21, 2004
    #2
    1. Advertising

  3. My suggestion is to check the length of string and use a for-loop to add
    appropiate amount of zeros the it.

    You may do it in server-side or client-side, depends on when you want to add
    the zeros and which one you feel more handy. Though I'll prefer to do it
    client-side as the user will know you've automatically added the leading
    zeros for it.

    Regards,
    Lau Lei Cheong

    "TN Bella" <> ???
    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!
    >
    >
    >
    >
    > *** Sent via Devdex http://www.devdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Lau Lei Cheong, Jun 21, 2004
    #3
  4. TN Bella

    TN Bella Guest

    If txtRefNum.Text.Length < 9 Then
    txtRefNum.Text = txtRefNum.Text.PadLeft(9, "0")
    End If


    I got this to work but I need to add more values (14 more) to the
    code...txtRefNum - txtRefNum14





    *** Sent via Devdex http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
    TN Bella, Jun 21, 2004
    #4
  5. In javascript you can supply the textcontrol's name as parameter and use
    eval() or document.getElementById() to do so. I don't know much in vbscript
    but you may try the Eval() function too.

    Dim myctrl = Eval("txtRefNum")

    If you can use it, you can supply a parameter to the function.

    Good luck. :)

    "TN Bella" <> ???
    news: ???...
    > If txtRefNum.Text.Length < 9 Then
    > txtRefNum.Text = txtRefNum.Text.PadLeft(9, "0")
    > End If
    >
    >
    > I got this to work but I need to add more values (14 more) to the
    > code...txtRefNum - txtRefNum14
    >
    >
    >
    >
    >
    > *** Sent via Devdex http://www.devdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Lau Lei Cheong, Jun 21, 2004
    #5
  6. use this:

    txtRefNum.Text = Format(Val(txtRefNum.Text), "0########")

    --
    Hope this helps,
    Zeeshan Mustafa, MCSD


    "Lau Lei Cheong" <> wrote in message
    news:...
    > In javascript you can supply the textcontrol's name as parameter and use
    > eval() or document.getElementById() to do so. I don't know much in

    vbscript
    > but you may try the Eval() function too.
    >
    > Dim myctrl = Eval("txtRefNum")
    >
    > If you can use it, you can supply a parameter to the function.
    >
    > Good luck. :)
    >
    > "TN Bella" <> ???
    > news: ???...
    > > If txtRefNum.Text.Length < 9 Then
    > > txtRefNum.Text = txtRefNum.Text.PadLeft(9, "0")
    > > End If
    > >
    > >
    > > I got this to work but I need to add more values (14 more) to the
    > > code...txtRefNum - txtRefNum14
    > >
    > >
    > >
    > >
    > >
    > > *** Sent via Devdex http://www.devdex.com ***
    > > Don't just participate in USENET...get rewarded for it!

    >
    >
    M. Zeeshan Mustafa, Jun 21, 2004
    #6
  7. If u want to use from javascript u can use this function on blur of textbox
    /*Events Fires on blur of TextBox*/
    function fnChange()
    {
    var obj;
    obj=document.getElementById('txtName'); //get the context of txtName element
    if (obj!=null)
    {
    if (Number(obj.value.length)<9)
    {
    Val=obj.value.toString(); //converts to string
    Val= '000000000' + Val ; //append 9 0's to the string
    obj.value=Val.substr(obj.value.length,Val.length); //gets the substring of Val.
    //like right function right(String,startindex)
    }
    }
    }
    if u want this should happen on post back u can use this
    txtRefNum.Text = Format(Val(txtRefNum.Text), "0########")



    "TN Bella" wrote:

    > 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!
    >
    >
    >
    >
    > *** Sent via Devdex http://www.devdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    >
    =?Utf-8?B?SEFSSSBQUkFTRCBCQVJV?=, Jun 21, 2004
    #7
    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. Teep
    Replies:
    2
    Views:
    752
    Larry Marburger
    Jun 21, 2004
  2. Replies:
    0
    Views:
    431
  3. Ann
    Replies:
    0
    Views:
    393
  4. Ann
    Replies:
    1
    Views:
    188
    Anthony Levensalor
    Jan 2, 2008
  5. Replies:
    4
    Views:
    85
    PyNoob
    Jun 27, 2013
Loading...

Share This Page