How to prevent the form from being sent if I have radiobuttons in NO

0

I'm making a newsletter form, I have 4 options all have Yes and No now I want to avoid that the form is sent if everything is in NO

This is my code, I tried it with Js but it did not work out. Thank you very much

<!-- Begin MailChimp Signup Form -->

<style type="text/css">
	#mc_embed_signup{background:#fff; clear:left; font:14px Helvetica,Arial,sans-serif; }
	/* Add your own MailChimp form style overrides in your site stylesheet or in this style block.
	   We recommend moving this block and the preceding CSS link to the HEAD of your HTML file. */
     tr,th,td{
      padding-left: : 15px;
      padding-right: 15px;
    
     }
     th{
      text-align: center;
     }
     p{
      line-height: 3;
     }
     table{}
     input{
      text-align: center;
     }
</style>
<div id="mc_embed_signup">
<form action="hqa.php" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" validate>
    <div id="mc_embed_signup_scroll">
	
<div class="mc-field-group input-group">
    <table width=100% align="center" style="border-spacing: 20px">
      <tr>
        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
          <input type="radio" value="Si" name="MMERGE1" id="mce-MMERGE1-0" checked> 
          <label for="mce-MMERGE1-0"> Si</label>  
          <input type="radio" value="No" name="MMERGE1" id="mce-MMERGE1-1"> 
          <label for="mce-MMERGE1-1"> No</label>
        </td>
        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
           <input type="radio" value="Si" name="MMERGE2" id="mce-MMERGE2-0" checked> 
           <label for="mce-MMERGE2-0">Si</label> 
           <input type="radio" value="No" name="MMERGE2" id="mce-MMERGE2-1"> 
           <label for="mce-MMERGE2-1">No</label>
        </td>
        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
              <input type="radio" value="Si" name="MMERGE3" id="mce-MMERGE3-0" checked> 
              <label for="mce-MMERGE3-0">Si</label> 
              <input type="radio" value="No" name="MMERGE3" id="mce-MMERGE3-1"> 
              <label for="mce-MMERGE3-1">No</label>
        </td>
        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
           <input type="radio" value="Si" name="MMERGE4" id="mce-MMERGE4-0" checked> 
           <label for="mce-MMERGE4-0">Si</label> 
           <input type="radio" value="No" name="MMERGE4" id="mce-MMERGE4-1"> 
           <label for="mce-MMERGE4-1">No</label>
        </td>
      </tr>
    </table> 
   

</div>

	<div id="mce-responses" class="clear">
		<div class="response" id="mce-error-response" style="display:none"></div>
		<div class="response" id="mce-success-response" style="display:none"></div>
	</div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
  <h3 align="center">Suscríbete a nuestro Newsletter</h3>
<div class="indicates-required"><span class="asterisk"></div>
<div class="mc-field-group" align="center">
  <input style="width: 400px" align="center" type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Ingresa tu correo electrónico">
</div>
    <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_adc1b7090103e09e24a7d0365_edeab16967" tabindex="-1" value=""></div>
    <div class="clear" align="center">
      <br>
      <br><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button"></div>
    </div>
</form>
</div>
<script type='text/javascript' src="http://code.jquery.com/jquery-1.7.2.min.js">
        $("#mc-embedded-subscribe-form").on('submit', function (evt) {

            if (!$("#mce-MMERGE1-1").is(':checked') && 
!$("mce-MMERGE2-1").is(':checked') && !$("#mce-MMERGE3-1").is(':checked') && !$("#mce-MMERGE4-1").is(':checked')) {
                evt.preventDefault();
                alert('No has marcado un Sí');
            }
        });
    </script>
</script>
    
asked by Rickzize 20.11.2017 в 19:11
source

2 answers

0

You could capture the change event of all the radios and if all the radios values are "No" you can un-enable the submit button or otherwise enable it.

<script type="text/javascript">
    
            $(document).ready(function () {
                $('input[type=radio][name=MMERGE1],input[type=radio][name=MMERGE2],input[type=radio][name=MMERGE3],input[type=radio][name=MMERGE4]').change(function () {
    
    
                    if ($("input[name='MMERGE1']:checked").val() == 'No' && $("input[name='MMERGE2']:checked").val() == 'No' && $("input[name='MMERGE3']:checked").val() == 'No' && $("input[name='MMERGE4']:checked").val() == 'No') {
                        $("#mc-embedded-subscribe").attr('disabled','disabled');
                    }
                    else  {
                        $("#mc-embedded-subscribe").removeAttr('disabled','disabled');
                    }
   
                });
            });
    
        </script>
    
answered by 20.11.2017 / 19:41
source
0

You could validate the shipment using return instead of preventDefault() in the following way:

$("#mc-embedded-subscribe-form").on('submit', function () {
    if ($("#mce-MMERGE1-1").is(':checked') && $("#mce-MMERGE2-1").is(':checked') && $("#mce-MMERGE3-1").is(':checked') && $("#mce-MMERGE4-1").is(':checked')) {   
        alert('No has marcado un Sí');

        return false;;
    }else{
        return true;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="mc_embed_signup">
    <form action="hqa.php" method="post" id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate" target="_blank" validate>
        <div id="mc_embed_signup_scroll">
    
            <div class="mc-field-group input-group">
                <table width=100% align="center" style="border-spacing: 20px">
                    <tr>
                        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
                            <input type="radio" value="Si" name="MMERGE1" id="mce-MMERGE1-0" checked> 
                            <label for="mce-MMERGE1-0"> Si</label>  
                            <input type="radio" value="No" name="MMERGE1" id="mce-MMERGE1-1"> 
                            <label for="mce-MMERGE1-1"> No</label>
                        </td>
                        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
                            <input type="radio" value="Si" name="MMERGE2" id="mce-MMERGE2-0" checked> 
                            <label for="mce-MMERGE2-0">Si</label> 
                            <input type="radio" value="No" name="MMERGE2" id="mce-MMERGE2-1"> 
                            <label for="mce-MMERGE2-1">No</label>
                        </td>
                        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
                            <input type="radio" value="Si" name="MMERGE3" id="mce-MMERGE3-0" checked> 
                            <label for="mce-MMERGE3-0">Si</label> 
                            <input type="radio" value="No" name="MMERGE3" id="mce-MMERGE3-1"> 
                            <label for="mce-MMERGE3-1">No</label>
                        </td>
                        <td align="center" colspan="1" width="300" style="border-color: #faf8f8" bgcolor=#faf8f8>
                            <input type="radio" value="Si" name="MMERGE4" id="mce-MMERGE4-0" checked> 
                            <label for="mce-MMERGE4-0">Si</label> 
                            <input type="radio" value="No" name="MMERGE4" id="mce-MMERGE4-1"> 
                            <label for="mce-MMERGE4-1">No</label>
                        </td>
                    </tr>
                </table> 
            </div>

            <div id="mce-responses" class="clear">
                <div class="response" id="mce-error-response" style="display:none"></div>
                <div class="response" id="mce-success-response" style="display:none"></div>
            </div>    <!-- real people should not fill this in and expect good things - do not remove this or risk form bot signups-->
            <h3 align="center">Suscríbete a nuestro Newsletter</h3>
            <div class="indicates-required"><span class="asterisk"></div>
            <div class="mc-field-group" align="center">
                <input style="width: 400px" align="center" type="email" value="" name="EMAIL" class="required email" id="mce-EMAIL" placeholder="Ingresa tu correo electrónico">
            </div>
            <div style="position: absolute; left: -5000px;" aria-hidden="true"><input type="text" name="b_adc1b7090103e09e24a7d0365_edeab16967" tabindex="-1" value=""></div>
            <div class="clear" align="center">
                <br>
                <br><input type="submit" value="Subscribe" name="subscribe" id="mc-embedded-subscribe" class="button">
            </div>
        </div>
    </form>
</div>
    
answered by 20.11.2017 в 19:49