Forums

This topic is locked

0 Padding a Text field

Posted 19 Sep 2008 17:34:15
1
has voted
19 Sep 2008 17:34:15 ed trvl posted:
Hi there,
I'm trying to format a user entry into a text field with leading 0s, the length of the field is 3 characters,:
The user input should be formated with 2 leading 0 for a single digits entry and one leading 0 for 2 digits inputed and none if the user inputs 3 digits:
eg input "7" becomes "007" in the field or
input "73" becomes "073" in the field or
input "736" becomes "736" in the field
The codes I'm trying to use don't seem to work for me,
Any help would be greatly appreciated, Thanks in advance
Code 1:
function zeroPad(num,count)
{
var numZeropad = num + '';
while(numZeropad.length < count) {
numZeropad = "0" + numZeropad;
}
return numZeropad;
}
HTML code: onkeypress="zeroPad(this, 3);"
Code 2:
function leadingZero2(x){
y=(x>9)?x:'00'+x;
y=(x>99)?x:'0'+x;
return y;
}

Reply to this topic