• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to zero fill field from html input

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an input field defined in html document. I want to make the field 8 characters, right justify the numbers entered and zero fill the leftmost bytes.

If user enters "1234" I want to use javascript to make field "00001234".
 
Don Martino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any ideas? Do you understand the question?
User enters 3 numbers into html input field. I want to use javascript client side to create 8 byte field, right justifying the numbers and fill the remaining 5 bytes to left with 0's.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should be quite easy to write a Javascript function which takes a string as a parameter and returns that same string, with zeroes appended at the left to make the length up to 8. Are you asking how to do that? Or how to get something to call that function? Don't know how to get it called at the right time? Or where to put the result?

So I guess, no, I don't really understand the question.
 
Don Martino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for response! I'm new to javascript and I'm trying to figure out how to do it, I mean creating string as parameter and appending 0's to left.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The basics:

onchange

document.formName.elementName.value

length

for loop

Eric
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is a easy function I made:

//STR_PAD_RIGHT = 1; STR_PAD_LEFT = 0;
function str_pad(input, pad_length, pad_string, pad_type){
for(i=input.length; i<pad_length; i++){
if(pad_type)
input = input + pad_string;
else
input = pad_string + input;
}
return input;
}

I hope it helps...
 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Don Martino wrote: I'm new to javascript and I'm trying to figure out how to do it



W3schools.com is your friend. It provides tutorials and reference manuals for many web technologies, including Javascript.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic