how the heck can i make this accept decimals ?

e.value=parseInt(e.value.replace(/[^\d]*/gi,\"\"));

can anyone recommend a site with javascript reference where this information can be found ?
LVL 1
aot2002Asked:
Who is Participating?
 
knightEknightConnect With a Mentor Commented:
for example:

  <INPUT type="text" name="numeric" onkeyup='this.onchange();' onchange='this.value=this.value.replace(/[^\d\.]*/gi,"");' />

this will not allow non-digit characters to be entered in the field.
0
 
RoonaanConnect With a Mentor Commented:
does javascript support parseDouble, because parseInt as the functionname says parses integers, which by default are rounded numerics.

-r-
0
 
darksinclairConnect With a Mentor Commented:
how many decimal accuracy do you want?

you can use parseFloat( )

to get floating point precision.

Cheers.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
aot2002Author Commented:
just a US dollar amount
0
 
aot2002Author Commented:
second place decimals
0
 
darksinclairConnect With a Mentor Commented:
0
 
KaarthickConnect With a Mentor Commented:
To accept decimals try parseFloat function
0
 
aot2002Author Commented:
e.value=parseFloat(e.value.replace(/[^\d]*/gi,\"\"));

doesnt work
0
 
aot2002Author Commented:
Comment from Kaarthick  feedback
Date: 09/21/2004 10:23AM EDT

No kidding ! i think i heard the first time
0
 
darksinclairCommented:
aot2002, Whats the error and what are you trying to do with the regular expression?

Check the link that I gave for a working example of parseFloat() function
0
 
cLFlaVAConnect With a Mentor Commented:
var val = Math.round(((500/100) * 2.8375643 + 500 * 100) / 100);

The above should give val a value of 2.84.
0
 
cLFlaVACommented:
Sorry, remove the +500.

var val = Math.round((2.8375643 * 100) / 100);
0
 
aot2002Author Commented:
im erasing any characters automatically except decimals and numbers
im using the text field as a dollar range
0
 
aot2002Author Commented:
i'd like to stick with replace function
0
 
aot2002Author Commented:
Comment from cLFlaVA  feedback
i'd like to stick with replace function
0
 
cLFlaVAConnect With a Mentor Commented:
Ok, but let me just correct my code for anyone else who may read this:

var the_float_number = 234.7896578;
alert(Math.round(the_float_number * 100) / 100);
0
 
darksinclairConnect With a Mentor Commented:
<SCRIPT>
var e = 2.3456;
e.value=parseFloat(e);
alert( e );
</SCRIPT>

^^ that works.  The problem is with your Regular Expression.  It is giving an invalid character error when you try to run it with your replace.

I am assuming that e is also a text box since you are doing e.value aswell.
0
 
Michel PlungjanIT ExpertCommented:
e=e.toFixed(2)
is another option
0
 
aot2002Author Commented:
what does to.fixed do ?
0
 
aot2002Author Commented:
that didnt work nor the float or the e.tofixed

e.tofixed says object doesnt support this property
and float says in alert box NaN
0
 
cLFlaVACommented:
var the_float = parseFloat(document.form_name.textbox_name.value);
alert(the_float.toFixed(2));
0
 
darksinclairCommented:
can you paste your code that you are working with.. You havent given us enough to know what your doing.. obviously your taking a text box in javascript and trying to do what with it?  What code do you have?  if your getting NaN with parseFloat() then you obviously still have characters in your text box which need to be removed.  (that's what your replace was trying to do but it has errors and will not work the way you want it too.

If you give us your code we may be able to help more. (complete code)
0
 
aot2002Author Commented:
what part of this expression is removing the decimal ????

e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));  
0
 
aot2002Author Commented:
im erasing any characters automatically except decimals and numbers
im using the text field as a dollar range

then on keyup i call this function





 function IsNumeric2(e)
{

        e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));

         if (isNaN(e.value) == true)
         e.value = '';

         e.focus();
       return false;




       }




0
 
aot2002Author Commented:
i have to step out brb in an hr
0
 
knightEknightCommented:
e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));

... but that will allow more than one . in the original number
0
 
darksinclairCommented:
<SCRIPT>
function doE()
{
      var e = document.getElementById( "e" ).value;
      e = parseFloat(e);
      alert( e );
      e = e.toFixed(2);
      alert( e );
}
</SCRIPT>
<FORM>
<INPUT TYPE="text" ID="e" name="e" value="2342.3343445">
<INPUT TYPE="button" value="do E!" onClick="doE();">
</FORM>

not sure what else to tell you here... the above works as long as the input is numeric,,, The problem is with your regular expression.  You need to fix that and get all teh characters out and then the above will do everything want.

Cheers.
0
 
aot2002Author Commented:
e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,\"\"));

What part of this expression actually tells it not to allow DECIMALS ?
0
 
knightEknightCommented:
it DOES allow decimals ... the problem is, it can potentially allow more than one :)
0
 
knightEknightCommented:
... at least with respect to the replace() method -- but the parseFloat wrapper might take care of that.
0
 
aot2002Author Commented:
well i cant seem to get it to allow decimals on my end ?
0
 
aot2002Author Commented:
i dont even get an error just cannot put in any decimals ?
0
 
knightEknightCommented:
sorry, there is a typo ... remove the backslashes:

e.value=parseFloat(e.value.replace(/[^\d\.]*/gi,""));
0
 
aot2002Author Commented:
this will not allow non-digit characters to be entered in the field.

ok GREAT how about getting it to allow DECIMALS too ?
0
 
knightEknightCommented:
it does!  :)
0
 
aot2002Author Commented:
you are correct but i still didnt understand why the function i called wouldnt allow decimals ?
ohh well
0
 
knightEknightCommented:
i didn't look at the function that carefully, sorry.  But parseFloat should be able to handle that well enough -- but again, I didn't test it.
I hope that was helpful -- thanks for the A.
0
 
aot2002Author Commented:
you deserved it !
after i stressed for hrs on somehting so simple !
0
 
Michel PlungjanIT ExpertCommented:
PS: I posted toFixed(2) not tofixed(2)  JS is case sensitive
0
 
knightEknightCommented:
what object does the toFixed method operate on?
0
 
Michel PlungjanIT ExpertCommented:
number only
a=1.555
b="2.555"
document.write('<b>'+a.toFixed(2)+'<br>'+parseFloat(b).toFixed(2)+'</b>')
0
 
knightEknightCommented:
thanks, I didn't know about that ...
0
 
aot2002Author Commented:
your right you did post to.Fixed
i missed typed there.

Anyway why would that say a object not supported on a textbox ?
or do you get this error on smaller case --> to.fixed ?
0
 
Michel PlungjanIT ExpertCommented:
Sorry I do not understand the question
0
 
knightEknightCommented:
I think what mplungjan mean was this:

   e.value = (1*e.value).toFixed(2)

instead of:

   e=e.toFixed(2)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.