This is a forum where members new and old can come to ask questions and get info and opinions. It is not a place to advertise your business or have other forms of advertising, whether it be in your posts or signature.

All links in the forum will not be indexed by Search Engines and any unapproved forms of advertising or spam will be dealt with accordingly, deleted, and that member's account banned.

Forums

Overview » Ajax & JavaScript » 0 Padding Text field
Reply

0 Padding Text field

ed trvl
Member



Since: 15 Sep 2008
Posts: 8
Posted 19 Sep 2008 17:32:07

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 doesn'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;
}

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 doesn'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

Message
Reply
Follow us on Facebook Follow us on twitter Subscribe to the RSS feed
Activate your free membership today | Login | Currency