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>
LVL 1
aot2002Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GwynforWebCommented:
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>
0
GwynforWebCommented:
i mean this

<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*/,'$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>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wzd3Commented:
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.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

wzd3Commented:
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.
0
GwynforWebCommented:
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>
0
aot2002Author Commented:
thanks man !
works fine and im only going to use the ie 6.0 Not netscape !
0
GwynforWebCommented:
thx for the points, :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

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.