[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

number format in form text input box

Posted on 2003-03-25
7
Medium Priority
?
269 Views
Last Modified: 2008-02-26
is there a tag that will automatically format data in a text input as a 2 decimal float?

example:
  enter 1; result 0.01
  enter 4; result 0.14
  enter 7; result 1.47

i can figure out the multiplication part to move the decimal, but i run into issues when adding more than 1 zero to the end.
0
Comment
Question by:cMetz
6 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 8206634
Here are a few places to look:

http://www.w3schools.com/js/js_math.asp

http://www.irt.org/xref/Math.htm

Fritz the Blank
0
 

Author Comment

by:cMetz
ID: 8206713
these help but do not address the bigger issue.  after re-reading my question i realize what i should have asked.  the main problem i run into is when you try to enter zeros.

if you try to enter 38.10, it shows up as 38.1 because the browser truncates all unneccessary digits.  i am looking for a way for the browser to understand that the data in that text field is to be understood as a float with 2 decimal places regardless of its value.
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 200 total points
ID: 8206749
if you always grab your values as parseFloat(), they will be interpreted as numbers, but I am not sure if that helps you.

Fritz the Blank
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 12

Expert Comment

by:lil_puffball
ID: 8207475
This will do the trick:

<script>
function format(obj){
text=obj.value.split('.').join('')
if(text.charAt(text.length-1)=='0')
{obj.value=text/100+'0'}
else
{obj.value=text/100;}
}
</script>

<form>
<input type="text" onKeyUp="format(this)">
</form>
0
 
LVL 1

Expert Comment

by:umangjoshi
ID: 8208290
you have two way

[1] consider you have a <form name="f".....
and a textfield name="no"

now in javascript pls try following

var num;
num = f.no.value * 1;
//this alert shows you which value stored in num
alert(num);

//if f.no.value is not a number, num contains value like NaN, may be other

if (num == NaN)
{
   .......
}
else
{
   ........



[2] define a function which check f.no.value, character by character and if any invalid symbole returns false (boolean)
}
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10149874
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: fritz_the_blank {http:#8206749}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question