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.ourc ost.value = ToDollarsAndCents(document .form_paym ent.ourcos t.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.rep lace(/[^\d \.]*/gi,"" );\' onblur="fixcurrency();"><b r>
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.ourc
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();"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.ourc ost.value = ToDollarsAndCents(document .form_paym ent.ourcos t.value);
return;
}
</script>
<body>
<form name="form_payment">
Our Cost <input type="text" name="ourcost" onkeyup="fixcurrency()" onchange="fixcurrency()"
</form>
<script>
function fixcurrency(){
function ToDollarsAndCents(n) {
n=n.replace(/[^\d\.+-]/g,'
if (/\d*\.\d*\./.test(n)) n=n.replace(/(\d*\.\d*)\./
n=n.replace(/(\.\d{2})[\d.
return n }
document.form_payment.ourc
return;
}
</script>
<body>
<form name="form_payment">
Our Cost <input type="text" name="ourcost" onkeyup="fixcurrency()" onchange="fixcurrency()"
</form>
ASKER
thanks man !
works fine and im only going to use the ie 6.0 Not netscape !
works fine and im only going to use the ie 6.0 Not netscape !
thx for the points, :-)
<script>
function fixcurrency(){
function ToDollarsAndCents(n) {
n=n.replace(/[^\d\.+-]/g,'
n=n.replace(/(\d*\.\d*)\./
n=n.replace(/(\.\d{2})\d*/
return n }
document.form_payment.ourc
return;
}
</script>
<body>
<form name="form_payment">
Our Cost <input type="text" name="ourcost" onkeyup="fixcurrency()"
onchange="this.value=(1*th
onblur="this.value=(1*this
</form>