Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 989
  • Last Modified:

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>
0
aot2002
Asked:
aot2002
  • 4
  • 2
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now