Link to home
Start Free TrialLog in
Avatar of aot2002
aot2002

asked on

onkeyup format field into this

i want the user to only be allowed to enter "One DECIMAL"
Only be allowed to enter "two places from the decimals to the right"
and only be allowed to enter "NUMERICS"


example of allowed numbers
1.22
34455.09
23.00
4343.00



here is my current code which doesnt want to work ?

function fixcurrency(){
        function ToDollarsAndCents(n) {
         var s = "" + Math.round(n * 100) / 100
            var i = s.indexOf()
            if (i < 0) return s + ".00"
            var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
            if (i + 2 == s.length) t += "0"
            return t
        }

         document.form_payment.ourcost.value = ToDollarsAndCents(document.form_payment.ourcost.value);
         return;
                               }


everytime i put in 1.22
i get the value in the text as 1.22.00
i know the todollarsandcents works though because i use it on another page ???
why is it causing issues now ?
im calling this from the textbox

        Our Cost <input type="text" name="ourcost" value="" onkeyup="this.onchange();" onchange=\'this.value=this.value.replace(/[^\d\.]*/gi,"");\' onblur="fixcurrency();"><br>
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

try tjhis instead
<script>
function fixcurrency(){
        function ToDollarsAndCents(n) {
          n=n.replace(/[^\d\.+-]/g,'')
          n=n.replace(/(\d*\.\d*)\./,'$1')
          n=n.replace(/(\.\d{2})\d*/,'$1')
           return n        }
        document.form_payment.ourcost.value = ToDollarsAndCents(document.form_payment.ourcost.value);
        return;
 }
</script>

<body>

<form name="form_payment">
Our Cost <input type="text" name="ourcost" onkeyup="fixcurrency()"
             onchange="this.value=(1*this.value).toFixed(2)"
             onblur="this.value=(1*this.value).toFixed(2);" size="20">

</form>
ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wzd3
wzd3

Or, for what is wrong with your code.  on the line


            var i = s.indexOf()

should have a "." as the argument.  That is
            var i = s.indexOf(".")

That is why the .00 is always added.

For general comments on your code, I would move the onchange code you have to the keyup code, and move the blur code to the onchange= case, and not use onblur.  onblur causes problems when you navigate to a different window, at least in netscape 7.2, so I recomment avoiding it.  It  has locked up netscape so I had to quit/restart in some cases, and your webpage will get the blame, fair or not.

And also, yours will not handle multiple decimal places.  I think Gwyn's will, so you might want to think about taking his cleanup routine.  I still dislike the onblur call though, even in his.
I do not think the onblur is needed. I found a wierd subcase not covered when pasting in, the onchange is needed for pasting. Corrected code:-

<script>
function fixcurrency(){
        function ToDollarsAndCents(n) {
          n=n.replace(/[^\d\.+-]/g,'')
          if (/\d*\.\d*\./.test(n)) n=n.replace(/(\d*\.\d*)\./,'$1')
          n=n.replace(/(\.\d{2})[\d.]*/g,'$1')
           return n        }
        document.form_payment.ourcost.value = ToDollarsAndCents(document.form_payment.ourcost.value);
        return;
 }
</script>

<body>

<form name="form_payment">
   Our Cost <input type="text" name="ourcost" onkeyup="fixcurrency()" onchange="fixcurrency()"
</form>
Avatar of aot2002

ASKER

thanks man !
works fine and im only going to use the ie 6.0 Not netscape !
thx for the points, :-)