开发者

JavaScript Validating Currency Amount

I am trying to validate a currency value in a shopping cart so that if it is less than $50 it hides 开发者_如何学Pythonthe 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>


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.


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.​

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜