[Webinar] Streamline your web hosting managementRegister Today

x
Solved

# number format in form text input box

Posted on 2003-03-25
Medium Priority
269 Views
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
Question by:cMetz

LVL 46

Expert Comment

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

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

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

LVL 12

Expert Comment

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

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

//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

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}

jAy
EE Cleanup Volunteer
0

## Featured Post

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
Course of the Month8 days, 15 hours left to enroll