Help! Need to add lead zeros to a textbox input

T

TN Bella

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!
 
G

Guest

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

Lau Lei Cheong

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
 
T

TN Bella

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!
 
L

Lau Lei Cheong

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. :)
 
G

Guest

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########")
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top