var currentTab = 0; // Current tab is set to be the first tab (0)
showTab(currentTab); // Display the current tab
function showTab(n) {
// This function will display the specified tab of the form...
var x = document.getElementsByClassName("tab");
x[n].style.display = "block";
//... and fix the Previous/Next buttons:
if (n == (x.length - 1)) {
document.getElementById("nextBtn").innerHTML = "Submit";
} else {
document.getElementById("nextBtn").innerHTML = "Next";
}
//... and run a function that will display the correct step indicator:
fixStepIndicator(n)
}
function nextPrev(n) {
// This function will figure out which tab to display
var x = document.getElementsByClassName("tab");
// Exit the function if any field in the current tab is invalid:
if (n == 1 && !validateForm()) return false;
// Hide the current tab:
x[currentTab].style.display = "none";
// Increase or decrease the current tab by 1:
currentTab = currentTab + n;
// if you have reached the end of the form...
if (currentTab >= x.length) {
// ... the form gets submitted:
document.getElementById("regForm").submit();
return false;
}
// Otherwise, display the correct tab:
showTab(currentTab);
}
function validateForm() {
// This function deals with validation of the form fields
var x, y, i, valid = true;
x = document.getElementsByClassName("tab");
y = x[currentTab].getElementsByTagName("input");
const button = document.getElementById()('button')
// A loop that checks every input field in the current tab:
for (i = 0; i < y.length; i++) {
// If a field is empty...
if (y[i].value == y.length) {
// add an "invalid" class to the field:
y[i].className += "invalid";
button.disabled = true
// and set the current valid status to false
valid = false;
} else {
button.disabled = false;
}
}
// If the valid status is true, mark the step as finished and valid:
if (valid) {
document.getElementsByClassName("step")[currentTab].className += " finish";
}
return valid; // return the valid status
}
function fixStepIndicator(n) {
// This function removes the "active" class of all steps...
var i, x = document.getElementsByClassName("step");
for (i = 0; i < x.length; i++) {
x[i].className = x[i].className.replace(" active", "");
}
//... and adds the "active" class on the current step:
x[n].className += " active";
}
* {
box-sizing: border-box;
}
body {
background-color: #ffffff;
user-select: none;
}
#regForm {
background-color: #ffffff;
margin: 100px auto;
font-family: Raleway;
padding: 1px;
width: 70%;
min-width: 300px;
}
h1 {
text-align: center;
}
input {
padding: 10px;
font-size: 17px;
font-family: Raleway;
border: 1px solid #aaaaaa;
}
label {
border: 1px solid #ccc;
padding: 20px;
margin: 0 0 10px;
display: block;
text-align: center;
cursor: pointer;
}
input[type=radio] {
display: none;
}
.tab input[type="radio"]:checked+label {
background-color: red;
color: #fff2f2;
}
/* Mark input boxes that gets an error on validation: */
input.invalid {
background-color: #ffdddd;
}
/* Hide all steps by default: */
.tab {
display: none;
}
.btn {
background-color: #ffffff;
color: #000000;
border: #000000 solid;
border-width: 1px;
width: 100%;
padding: 10px 20px;
font-size: 17px;
font-family: Raleway;
cursor: pointer;
}
button:focus {
background-color: #000000;
border-width: 1px;
color: #ffffff;
}
button:hover {
opacity: 0.8;
}
#prevBtn {
background-color: #bbbbbb;
}
/* Make circles that indicate the steps of the form: */
.step {
height: 15px;
width: 15px;
margin: 0 2px;
background-color: #bbbbbb;
border: none;
border-radius: 50%;
display: inline-block;
opacity: 0.5;
}
.step.active {
opacity: 1;
}
/* Mark the steps that are finished and valid: */
.step.finish {
background-color: #04AA6D;
}
<form id="regForm" action="" method="GET">
<h1>Register:</h1>
<!-- One "tab" for each step in the form: -->
<div class="tab">
<!-- name should same for each group and value should different it will based on option value (we select) -->
<input type="radio" id="1" name="Gender" value="Male">
<label for="1">Male</label>
<input type="radio" id="2" name="Gender" value="Female">
<label for="2">Female</label>
<input type="radio" id="3" name="Gender" value="Other">
<label for="3">Other</label>
</div>
<div class="tab">
<input type="radio" id="4" name="Hobbies" value="Reading">
<label for="4">Reading</label>
<input type="radio" id="5" name="Hobbies" value="Dancing">
<label for="5">Dancing</label>
<input type="radio" id="6" name="Hobbies" value="Nothing">
<label for="6">Nothing</label>
</div>
<div class="tab">
<input type="radio" id="7" name="Sports" value="Cricket">
<label for="7">Cricket</label>
<input type="radio" id="8" name="Sports" value="Football">
<label for="8">Football</label>
<input type="radio" id="9" name="Sports" value="Volleyball">
<label for="9">Volleyball</label>
</div>
<div class="tab">
<input type="radio" id="10" name="Living" value="Home">
<label for="10">Home</label>
<input type="radio" id="11" name="Living" value="PG">
<label for="11">PG</label>
<input type="radio" id="12" name="Living" value="Hostel">
<label for="12">Hostel</label>
</div>
<!-- Submit working only if having previous button -->
<div style="overflow:auto;">
<div style="float:right;">
<button type="button" class="submit" id="nextBtn" onclick="nextPrev(1)" disabled>Next</button>
</div>
</div>
<!-- Circles which indicates the steps of the form: -->
<div style="text-align:center;margin-top:40px;">
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
<span class="step"></span>
</div>
Hello I'm working on form validation of radio buttons. I have a very long form and I am validating each field of that form. The pasted one is some of it. Requirement: I need help in form validation of radio buttons. I want to disable next button until radio button is checked I tried many ways but not getting exact results as my requirement. Please help to solve me this. Thanks in advance.
Note: If possible in jQuery please do the need. Thanks