Please submit the form only after checking the appropriate checkbox

I am encountering an issue with form validation.

In the first image, users can make a choice that will determine the display of either form 2 or form 3 (shown in images 2 and 3).

The fields in each form are marked as mandatory but both sections start as display: none. They only appear once the user clicks on one of the two buttons.

How can I ensure that only one form is activated based on the user's choice?

When I try to submit while filling out the fields in the first form, it prompts for the required fields in the second form as well.

https://i.sstatic.net/atLnB.png https://i.sstatic.net/ppxBL.png https://i.sstatic.net/HYr1e.png

Thank you in advance

Here is the jQuery code to show the button:

$( "#button-ritira" ).click(function() {
              $( "#ricevi" ).hide( 1000 );
              $( "#ritira" ).show( 400 );
            });

            $( "#button-ricevi" ).click(function() {
              $( "#ritira" ).hide( 1000 );
              $( "#ricevi" ).show( 400 );
            });

This is the HTML code:

<div class="row mb-45">
            <div  class="col-lg-12">
                <div class="row">
                    <div class="col-lg-4 offset-lg-2 text-center">
                        <label class="label-space-radio-ritira mb-25">
                          <input type="radio" name="ritira" id="button-ritira" autocomplete="off">
                          <span class="radiocustom-ritira"></span>
                        </label>
                    </div>
                    <div class="col-lg-4 text-center">
                            <label class="label-space-radio-ritira mb-25">
                              <input type="radio" name="ritira" id="button-ricevi" autocomplete="off">
                              <span class="radiocustom-ricevi"></span>
                            </label>
                      </div>
                  </div>
            </div>
        </div>

        <div class="row" id="ritira">
            <div class="col-lg-12">
                <div class="row">
                    <div class="col-lg-4 offset-lg-2">
                            <div class="input-form-ricevi-ritira">
                                <label>Name</label>
                                <input type="text" name="name" placeholder="Name" required autocomplete="off">
                            </div>
                            <div class="input-form-ricevi-ritira">
                                <label>Surname</label>
                                <input type="text" name="surname" placeholder="Surname" required autocomplete="off">
                            </div>
                            <div class="input-form-ricevi-ritira">
                                <label>Email</label>
                                <input type="text" name="email" placeholder="Email" required autocomplete="off">
                            </div>
                            <div class="input-form-ricevi-ritira">
                                <label>Mobile</label>
                                    <input type="text" name="mobile" placeholder="Mobile" required autocomplete="off">
                            </div>
                    </div>
                    <div class="col-lg-4">
                            ...
                        </div>
                </div>
            </div>
        </div>

        <div class="row" id="ricevi">
            <div class="col-lg-12">
                <div class="row">
                    <div class="col-lg-4 offset-lg-2">
                            ...
                    </div>
                    <div class="col-lg-4">
                            ...
                    </div>
                </div>
            </div>
        </div>

Answer №1

Aside from the hide and show functions, consider adding a disabled class to all input fields as form one, and another disabledtwo class for form two. When toggling between showing and hiding the forms, ensure that the input fields of the inactive form are disabled so that no data can be submitted.

You can view the demonstration where the fields remain disabled even when not hidden.

If, by some chance (although doubtful), the required fields still prompt submission requests, you can add or remove the 'required' attribute on them in a similar way:

$( "#button-ritira" ).click(function() {
              //$( "#ricevi" ).hide( 1000 );
              //$( "#ritira" ).show( 400 );
              $(".disabled").prop('disabled', true);
              $(".disabledtwo").prop('disabled', false);
            });

            $( "#button-ricevi" ).click(function() {
              //$( "#ritira" ).hide( 1000 );
              //$( "#ricevi" ).show( 400 );
              $(".disabledtwo").prop('disabled', true);
              $(".disabled").prop('disabled', false);
            });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row mb-45">
            <div  class="col-lg-12">
                <div class="row">
                    <div class="col-lg-4 offset-lg-2 text-center">
                        <label class="label-space-radio-ritira mb-25">
                          <input type="radio" name="ritira" id="button-ritira" autocomplete="off">
                          <span class="radiocustom-ritira"></span>
                        </label>
                    </div>
                    <div class="col-lg-4 text-center">
                        <label class="label-space-radio-ritira mb-25">
                          <input type="radio" name="ritira" id="button-ricevi" autocomplete="off">
                          <span class="radiocustom-ricevi"></span>
                        </label>
                    </div>
                </div>
            </div>
        </div>

... (remaining HTML code) ...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize the technique on the designated variable

I need to modify a variable to use a method in the following way; var method = ".wrap"; jQuery('.container')+[method]+("<div>Hello World</div>"); The desired outcome is; jQuery('.container').wrap("<div>Hello Worl ...

Access a webpage outside of your domain using htaccess and PHP functionality

When a request is made to my website like this: my1stdomain.com/request-1/ my1stdomain.com/any-another-request/ The corresponding web pages should open on: my2nddomain.com/folder/request-1.php my2nddomain.com/folder/any-another-request.php To achie ...

How can jQuery be employed to locate the position of an element within its group of siblings with a particular CSS class?

Here is an example of HTML code: <div class="component"> <div class="component"> <div class="component"> </div> </div> <div class="component"> <div class="somethingelse"> ...

Mega Dropdown Menu using CSS

I am currently working on a dynamic mega drop-down menu using only HTML and CSS. The issue I'm facing is that when I select one of the list items (LIs), it expands in size at the expense of the other LIs. I'm not sure where the problem lies, so ...

Every time I attempt to utilize pdf.js, I encounter a Cross-Origin Request block

When attempting to read a local PDF file using pdf.js, I included the following script tag: <script src="https://cdn.jsdelivr.net/npm/pdfjs/build/pdf.min.js"> </script> To check if it was working, I added a simple console.log: ...

The CSS cubic-bezier fails to smoothly transition back to its initial position

I created an animation that works perfectly when hovering over the target elements, however, it doesn't smoothly transition back to its original position when the cursor moves outside of the target element. Instead, it snaps back abruptly and unattrac ...

Employing the "div" element to target in CSS styling

I'm dealing with a document structure that is consistent across multiple pages. HTML: <ul> <li> <div> <img src="" /> </div> </li> </ul> Presently, there is a border aroun ...

What are the different ways to programmatically change CSS rules using JavaScript in a Firefox Add-on, leveraging XUL, SDK, or WebExtensions methods?

Looking to dynamically alter a CSS rule set through JavaScript within a Firefox Add-on using XUL, SDK or WebExtensions techniques. Specifically targeting support for Firefox versions 29.0b1 through 49.0a1. The main issue I want to address is modifying the ...

Event fails to trigger when attached to different elements

Currently, I have an onchange event linked to the input text box and an onclick event attached to a text link. Interestingly, when I click on the link after editing the textbox, the click event does not trigger - instead, the change event is fired. If you ...

Encountering an error while utilizing ngForm in an HTML form

I have developed a custom component called "login", where I created an HTML form named "login.component.html". To convert this form into an Angular form, I added the code "" in login.component.html and imported import {NgForm} from '@angular/forms&apo ...

How to retrieve the content/value from a textfield and checkbox using HTML

I'm encountering an issue with my HTML code where I am unable to extract data from the HTML file to TS. My goal is to store all the information and send it to my database. Here is a snippet of the HTML: <h3>Part 1 : General Information</h3 ...

PHP and JavaScript: Understanding Variables

I currently have a View containing an Associative Array filled with information on accidents. Users will have the ability to click on a Country. Once clicked, I want to display accident-related data for that specific country. This data is pulled from PHP ...

Is the HTML encoded character displaying strangely due to spacing issues?

I'm having trouble creating a link with a right chevron that has a larger font size. The issue I'm facing is that the right chevron appears to have excessive top margin, causing a significant gap between it and the line above. Additionally, I wa ...

What is the best way to pass a variable in PHP and create an HTML link to a specific element ID simultaneously?

Formulating the question may be challenging, but the concept is quite straightforward: I'd like to pass the variable 'count' using GET and simultaneously direct to the media section. On the page http://page.org.mx/comunicados#media I&apo ...

Guide on executing edit form alongside file upload

Is there a way to update a form without having to upload a new image? I have successfully implemented multer for image uploads when creating a form. My frontend is built using ReactJS and the backend server side is Node.js. This is the front end code usi ...

Progress Bar Countdown Timer

I have made some progress on my project so far: http://jsfiddle.net/BgEtE/ My goal is to achieve a design similar to this: I am in need of a progress bar like the one displayed on that site, as well as the ability to show the days remaining. Additionally ...

Locate and substitute text, language equivalency

I'm encountering issues with finding and replacing words in PHP files, especially when dealing with a large number of them. I'm considering using javascript/jQuery as an alternative solution. I want to create a table mapping word_to_replace to ne ...

I'm attempting to execute a piece of code that retrieves a status count from a dataframe, the output of which will be displayed above my pandas html table

#Here is an example of a dataframe: import pandas as pd import numpy as np data = [['001', 'Completed'], ['002', 'In Process'], ['003', 'Not Started'], ['004''Completed ...

Prevent HTML escaping inside of a code tag in JavaScript

Is there a way to search for tags "<" and ">" within a <code> block, setting them as text instead of HTML so that all tags and HTML can be displayed on the page? Currently, when I use jQuery to do a string replace, all tags are coming through ...

Enhancing the appearance of Angular Material components through nested components

HomeComponent has templates that include a router outlet /* home.component.ts */ ... <mat-sidenav-container> <mat-sidenav-content> <router-outlet></router-outlet> </mat-sidenav-content> </mat-sidenav-container&g ...