How do I stop a form reducing to Zero value?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I stop a form reducing to Zero value?

Post by simonmlewis »

Hi

I am trying to make this + - form prevent it going down to a zero (0).
Cannot quite see how to do that. I'm sure it can be done within its existing Javascript.

Can anyone offer some guidance please.

Code: Select all

<Script>$(window).load(function(){
jQuery(document).ready(function(){
    // This button will increment the value
    $('.qtyplus').click(function(e){
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If is not undefined
        if (!isNaN(currentVal)) {
            // Increment
            $('input[name='+fieldName+']').val(currentVal + 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    });
    // This button will decrement the value till 0
    $(".qtyminus").click(function(e) {
        // Stop acting like a button
        e.preventDefault();
        // Get the field name
        fieldName = $(this).attr('field');
        // Get its current value
        var currentVal = parseInt($('input[name='+fieldName+']').val());
        // If it isn't undefined or its greater than 0
        if (!isNaN(currentVal) && currentVal > 0) {
            // Decrement one
            $('input[name='+fieldName+']').val(currentVal - 1);
        } else {
            // Otherwise put a 0 there
            $('input[name='+fieldName+']').val(0);
        }
    });
});

});//]]>  
</script>

<form action='http://www.site.com/cart.asp' method='post' id='myform'>
<input type=hidden name=storeid value=11111>
<input type=hidden name=itemname2 value=trade>
<input type=hidden name=itemcode value='$row->romancode'>";
echo "
<div class='product_carton'>carton quantity</div>

<input type='button' value='-' class='qtyminus' field='quantity' />
    <input type='text' name='quantity' value='5' class='qty' />
    <input type='button' value='+' class='qtyplus' field='quantity' />
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I stop a form reducing to Zero value?

Post by Celauran »

Without testing it, I assume it's going down to zero but not below?
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I stop a form reducing to Zero value?

Post by simonmlewis »

Correct.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I stop a form reducing to Zero value?

Post by simonmlewis »

Hold on - what you jsut said made me wonder.
I just tweaked the final bit of code... to be 1, rather than 0. Easy I suppose.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I stop a form reducing to Zero value?

Post by simonmlewis »

And readonly on the field to stop it being typed in.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply