Homnath Bagale
Homnath Bagale

Reputation: 474

how to stop form submit and throw error jquery

Hi I have button in popup that submits the payment gateway information. All I want is when payment method (radio button) or accept terms (check box) are not checked i have to throw error, if any one is choosed then throw error to check another and if both are checked then open new mini browser. where I have used AJAX to submit these values .

$(document).ready(function() {


    $(document).on("mouseover", ".imgpayment", function() {
        $(this).parent().addClass("paymenthover");
    });

    $(document).on("mouseout", ".imgpayment", function() {
        $(this).parent().removeClass("paymenthover");
    });

    $(document).on("click", ".the-terms", function() {
        if ($(this).is(":checked")) {
            $("table#paymenttable td").each(function() {
                if ($(this).hasClass("paymentclick")) {
                    $(this).removeClass("paymentclick");
                }
            });
            if ($("#policyAgree").prop('checked') == true) {
                $("#submitBtn").removeAttr("disabled");
            } else {
                $("#paymentError").html('<span class="disablebtnInfo" id="msg" style="display:block;margin-left: 40%;"><span class="error_sign">!</span>&nbsp;<span id="errmsg">Agree terms and condition.</span></span></strong>');

            }
            $(this).parent().addClass("paymentclick");
        } else {
            $("#paymentError").html('<span class="disablebtnInfo" id="msg" style="display:block;margin-left: 40%;"><span class="error_sign">!</span>&nbsp;<span id="errmsg">Agree terms and condition.</span></span></strong>');
            $("#submitBtn").attr("disabled", "disabled");
        }
    });


    $(document).on("click", ".imgpayment", function() {
        $("table#paymenttable td").each(function() {
            if ($(this).hasClass("paymentclick")) {
                $(this).removeClass("paymentclick");
            }
        });
        if ($("#policyAgree").prop('checked') == true) {

        } else {
            $("#paymentError").html('<span class="disablebtnInfo" id="msg" style="display:block;margin-left: 40%;"><span class="error_sign">!</span>&nbsp;<span id="errmsg">Agree terms and condition.</span></span></strong>');

        }
        $(this).parent().toggleClass("paymentclick");
        $(this).parent().find(".the-terms").prop('checked', true);
        checkterms();
    });


});


$(document).on("click", "#policyAgree", function() {
    if ($(this).prop('checked') == true) {
        checkterms();
    } else {
        $("#submitBtn").attr("disabled", 'disabled');
    }

});

function checkterms() {
    if ($(".the-terms").is(":checked") && $("#policyAgree").is(":checked")) {
        $("#submitBtn").removeAttr("disabled");
    } else {
        $("#submitBtn").attr("disabled");
    }
}

and html:

<tr id="paymentmethod">
    <td>
        <input type="radio" name="payment" value="paypal" class="the-terms" required>
        <img src='<?php echo base_url() . ' contents/images/paypal.png '; ?>' class="imgpayment" style="height: 40px;">
    </td>
</tr>
<tr>
    <td>
        <input type="radio" name="payment" value="esewa" class="the-terms" required>
        <img src='<?php echo base_url() . ' contents/images/esewa.png '; ?>' class="imgpayment" style="height: 40px;">
    </td>
</tr>
<?php } ?>
<tr>
    <td style="height: 50px;">
        <input type="radio" name="payment" value="counterPay" class="the-terms" required>
        <span class="imgpayment" style="font-weight: bold"> Cash On Arrival </span>
    </td>
</tr>
<div id="action">
    <input id="type" type="hidden" name="mini" value="mini" />
    <input id="paykey" type="hidden" name="paykey" value="10" />
    <input type="button" id="popupBtn" value="Back" onclick="backbutton()" class="backBtnPayment" />
    <span id="paymentError" style="color:#0d0daf;font-size: 12px;float: left;"><?php if(!empty($msgs)){ echo $msgs; } ?></span>
    <input type="submit" id="submitBtn" value="Next" class="payment" disabled style="float: right;">
</div>

Upvotes: 1

Views: 2589

Answers (4)

Akshay Khale
Akshay Khale

Reputation: 8351

You may use submit event of form for form validation like this

$( "#form1" ).submit(function(event){
    if(FormNotvalid){
        alert("Error");
        event.preventDefault();
    }
});

checkout this simple example for your reference

<html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $( "#form1" ).submit(function(event){
                if($("#email").val() == ""){
                    alert("Error");
                    event.preventDefault();
                }
            });
        })
    </script>
    </head>
    <body>
        <form id="form1" action="#">
            <pre>
                <input type="text" id="email" name="email"/>
                <input type="submit"/>
            </pre>
        </form>
    </body>
</html>

Upvotes: 0

Vijay Porwal
Vijay Porwal

Reputation: 152

Try to use Jquery validation and inside submitHandler you can call ajax request

Jquery validation example

Upvotes: 0

user4967637
user4967637

Reputation:

When click submitBtn, you can stop it by function preventDefault such as:

$("#submitBtn").click(function(event){
      event.preventDefault();
      /// check validate conditions
      if(validate)
        $("#formID").submit();
      else
         throw errors;
});

Upvotes: 3

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

you can put onsubmit function call on form and return false for validation failed.

HTML:

<form onsubmit="validate()"....>

javascript:

 function validate()
{
  //your logic to validate data
  if(valid data)
    return true;
  else
   return false;
}

Upvotes: 2

Related Questions