tags:

views:

32

answers:

2

Hi,

I am trying to validate a currency value in a shopping cart so that if it is less than $50 it hides the check out button. I have used the same script for min order units and it worked, but not for currency. Code below

<script type="text/javascript">
setTimeout ("checkout()", 10 );
function checkout() {
    var tu = document.getElementById('cost').innerHTML;
    if (tu < 50) {
        document.getElementById('buyvalidation').style.display='none';
        document.getElementById('no').style.display='block';
    } else {
        document.getElementById('buyvalidation').style.display='block';
    }
    setTimeout ( "checkout()", 10 );
}
</script>
+1  A: 

Try

    var tu = parseFloat(document.getElementById('cost').innerHTML);

-- I think your comparison isn't working right due to conversion issues.

Also, does the cost element include the dollar sign? (Or currency symbol.) If so, then you should strip it from the element. There could also be an issue of when the cost field has no data in it.

Larry K
The `<` operator should take care of the string conversion.
patrick dw
A: 

What is the value of tu?

If it has $ and ,, you should strip those out.

if( tu.replace(/[$,]/g, '') < 50 ) {...

Although it is probably better practice to have a related hidden field that just has the number without any decorations.​

patrick dw
Thanks mate, that did the trick. (TU did contain a $). Much appriciated.
Ben Taine