본문 바로가기

Web Dev/Javascript

Javascript - Radio input 태그- 필수 항목 미 선택 시 false 반환 코드

반응형

<tr>
<th>개인(기업)/팀</th>
<td id="teamSelection">
<c:choose>
<input type="radio" name="wr_1" onclick="selectTeam('0','1')" <% out.println(readonly); %>> 1
<input type="radio" name="wr_1" onclick="selectTeam('1','')" <% out.println(readonly); %>> 2
</c:choose>

</td>
</tr>

<script>
function validateForm() {
	var radioChecked = false;
	var radios = document.getElementsByName('wr_1');
	for (var i = 0; i < radios.length; i++) {
		if (radios[i].checked) {
			radioChecked = true;
			break;
		}
	}

	if (!radioChecked) {
		alert("1 / 2을 선택해주세요.");
		return false; // Prevent form submission
	}

	return true; // Allow form submission
}
</script>
반응형