3

I have a function to sum values a form and compare with a another value.

Fiddle HERE.

HTML :

<input class="fieldToValidate" name="option-quantity">
<input class="fieldToValidate" name="option-quantity">
<input class="fieldToValidate" name="option-quantity">
<input class="fieldToValidate" name="option-quantity">

<button type='button' id="button-cart">Add To Cart</button>
<div class="errorQuantity"></div>  

JS :

$('#button-cart').on('click', function()
{
    var MaxSelectionNum = "7";
    var sum = 0;

    // Loop through all inputs with names that start
    // with "option-quantity" and sum their values
    $('input[name^="option-quantity"]').each(function()
    {
        var val = $(this).val() || 0;
        if(val != 0) {
          val = val.replace('.', '');
          val = val.replace(',', '.');
          val = parseFloat(val);
        }

        console.log(val);
        sum += val;
    });

    if (sum < MaxSelectionNum)
    {
        $(".errorQuantity").html("Please select 7 meals").show();
    }
    else
    {
        $(".errorQuantity").html("You have select greater than 7: meal count:  " + sum).show();
    }
});

This work, but i want to validate form before submit.

If the sum < 7, do not submit.

How can i do it ?

Thanks!

2 Answers 2

1

You have to put your inputs inside a form and call submit in your script after validation.

HTML :

<form ip='my-form'>  
    <input class="fieldToValidate" name="option-quantity">
    <input class="fieldToValidate" name="option-quantity">
    <input class="fieldToValidate" name="option-quantity">
    <input class="fieldToValidate" name="option-quantity">

    <button type='button' id="button-cart">Add To Cart</button>
    <div class="errorQuantity"></div>  
</form>

JS :

if (sum < MaxSelectionNum)
{
    $(".errorQuantity").html("Please select 7 meals").show();
}
else
{
    $('#my-form').submit();
}    

NOTE : You should change the names of inputs.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

You can trigger the validation function on .submit() event of the form.

<form id='my-form' action="your action goes here">  
    <input class="fieldToValidate" name="option-quantity">
    <input class="fieldToValidate" name="option-quantity">
    <input class="fieldToValidate" name="option-quantity">
    <input class="fieldToValidate" name="option-quantity">

    <input type="submit" value="Add to Card" />
    <div class="errorQuantity"></div>  
</form>

    $(function(){
    $("#my-form").submit(function(){
        var maxSelectionNum = "7";
        var sum = 0;
        $("input:text[name='option-quantity']").each(function(index, val) {
            sum += parseInt(this.value, 10);
        });

        if (sum >= maxSelectionNum) {
            return true;
        } else {
            $(".errorQuantity").html("Please select 7 meals").show();
            return false;
        }
    });
});

JSFIDDLE

1 Comment

Can you check why form continue if the sum is not the total value ? jsfiddle.net/La18Lcns/17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.