Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pad leading 0 in front of string 1

Status
Not open for further replies.

jnyvtka

Programmer
Feb 27, 2002
5
US
Hello I have a form in which a user may enter 0-5 characters. If the user enters less then 5 numbers in this field I need to pad it with 0's to make it 5. Can anyone help me out with this or lead me in the right direction here is what I have tried so far and nothing....

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=iso-8859-1&quot;>
</head>
<script language=&quot;JavaScript&quot;>
function zeroPad() { // pad string

if (document.forms1.C4.length == 1) {
(0000 + document.forms1.C4.value);
}
}

</script>
<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;&quot;>
<p>
<input type=&quot;text&quot; name=&quot;C4&quot; onChange=&quot;zeroPad()&quot;>
</p>
<p>
<input type=&quot;text&quot; name=&quot;textfield&quot;>
</p>
</form>
</body>
</html>


I am not a javascript programmer so please forgive this question as it is probably very easy to write however I do no have a good command of the language.

Thanks-
K.
 
document.forms1.C4.value = 0000 + document.forms1.C4.value;

i think thats your problem. hope it helps Suceess, thats the way you spell success!
Got a question? Ask IMOZRMY!
 
In the script section of your page put this function in:

Code:
function padNumber(oPadder)
{
	sPadThis = oPadder.value;
	sPadding = &quot;00000&quot;;
	sPadded = sPadding.substr(0, sPadding.length - sPadThis.length) + sPadThis;
	oPadder.value = sPadded;
}

Change your input tag to be:
Code:
<input type=&quot;text&quot; name=&quot;C4&quot; onChange=&quot;padNumber(this)&quot;>
 
!1 that worked thanks for the response.

I think I can follow everyting in that function so I learned something also.....
 
How about

while (document.forms1.C4.value.length < 5)
{
document.forms1.C4.value='0' + document.forms1.C4.value
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top