So if both validations work seperatley (if I just put radio buttons validation in the function without the spinner validation then radio buttons validation works and same vise versa), how come when I put both validations in the function, the top validation which is the radio validation works but the bottom validation which is the spinner validation does not display a message when it is suppose to?
Code is below:
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Create a Session</title>
<script type="text/javascript">
function validation() {
var errMsgO = document.getElementById("radioAlert");
var btnRadioO = document.getElementsByName("sessionNo");
var isbtnRadioChecked = false;
var errQuesMsgO = document.getElementById("numberAlert");
var questionNumberO = document.getElementsByName("txtQuestion");
for(i=0; i < btnRadioO.length; i++){
if(btnRadioO[i].checked){
isbtnRadioChecked = true;
}
}
if(!isbtnRadioChecked) {
errMsgO.innerHTML = "Please Select the Number of Sessions you Require";
} else {
errMsgO.innerHTML = "";
}
return false;
if(questionNumberO[0].value == 0){
errQuesMsgO.innerHTML = "Please Set the Number of Questions";
} else {
errQuesMsgO.innerHTML = "";
}
return false;
}
</script>
</head>
<body>
<form action="create_session.php" method="post" name="sessionform">
<table>
<tr>
<th>2: Number of Sessions :</th>
<td class="sessionNo"><input type="radio" name="sessionNo" value="1" />1</td>
<td class="sessionNo"><input type="radio" name="sessionNo" value="2" />2</td>
<td class="sessionNo"><input type="radio" name="sessionNo" value="3" />3</td>
<td class="sessionNo"><input type="radio" name="sessionNo" value="4" />4</td>
<td class="sessionNo"><input type="radio" name="sessionNo" value="5" />5</td>
</tr>
</table>
<div id="radioAlert"></div>
<table>
<tr>
<th>Number of Questions:</th>
<td class="spinner"><textarea class="spinnerQuestion" name="txtQuestion" id="txtQuestion" cols="2" rows="1"></textarea></td>
<td><button class="scrollBtn" id="btnQuestionUp"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button>
<button class="scrollBtn" id="btnQuestionDown"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td>
</tr>
</table>
<div id="numberAlert"></div>
<p><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="validation()" /></p> <!-- Prepare Questions here-->
</form>
</body>
The one mistake I could spot is the line if(questionNumberO == 0){
(besides you have not closed your <script>
element with </script>
).
The correct way is:
if(questionNumberO[0].value == 0){
Why:
getElementsByName()
will return a collection of elements with the specified name
attribute. If you know there is only one, you can ask for the first one ([0]
).
The value
property stores the current value of an input element. See HTMLInputElement on MDN.
Update: I have created a working jsFiddle Demo for you.