• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
Sheriff
Posts: 28326
96
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.
 
There is no "i" in denial. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic