I am writing a form validation code in PHP. Below is the code.
the element with id #questionSubmit is a form with 6 text fields (code, question, answer, option1, option2, option3, option4, option5) and a submit button.
<form id="createQuestionForm" action="" method="POST">
Question Code: <input id="code" class="createQuestionTextBox1" type="text" name="questionCode">
Question Name: <input id="question" class="createQuestionTextBox1" type="text" name="questionName">
Correct Answer: <input id="answer" class="createQuestionTextBox1" type="text" name="correctAnswer">
Option 1: <input id="option1" class="createQuestionTextBox2" type="text" name="option_1">
Option 2: <input id="option2" class="createQuestionTextBox2" type="text" name="option_2">
Option 3 <input id="option3" class="createQuestionTextBox2" type="text" name="option_3">
Option 4 <input id="option4" class="createQuestionTextBox2" type="text" name="option_4">
Option 5 <input id="option5" class="createQuestionTextBox2" type="text" name="option_5">
<input type="Submit" id="questionSubmit" value="Create Question"></input>
</form>
function SubmitFormCreationData() {
$("#questionSubmit").click(function() {
if (CheckCodeField($("#code").val()) == false) {
return false;
} else if (CheckAnswerNameFields($("#question").val()) == false) {
return false;
} else if (CheckCorrectAnswerField($("#answer").val()) == false) {
return false;
} else if (CheckAnswerNameFields($("#option1").val()) == false) {
return false;
} else if (CheckAnswerNameFields($("#option2").val()) == false) {
return false;
} else if (CheckAnswerNameFields($("#option3").val()) == false) {
return false;
} else if (CheckAnswerNameFields($("#option4").val()) == false) {
return false;
} else if (CheckAnswerNameFields($("#option5").val()) == false) {
return false;
} else {
$.post("InsertNewQuestion.php", $('#createQuestionForm').serialize());
alert('Quiz Created');
window.setTimeout(delay,2000);
return true;
}
return false;
});
}
function CheckAnswerNameFields(value) {
var isValid = true;
if (value == "")
isValid = false;
if (value == null)
isValid = false;
for(LCV = 0;LCV <= (count(value)-1); LCV++) {
if(value[LCV] == "'")
isValid = false;
if(value[LCV] == '"')
isValid = false;
}
return isValid;
}
function CheckCodeField(value) {
var isValid = true;
if(isInteger(value) == false)
isValid = false;
if(value < 100000)
isValid = false;
if(value > 999999)
isValid = false;
return isValid;
}
function CheckCorrectAnswerField(value) {
var isValid = true;
if(isInteger(value) == false)
isValid = false;
if(value < 1)
isValid = false;
if(value > 5)
isValid = false;
return isValid;
}
function isInteger(possibleInteger) {
return /^[\d]+$/.text(possibleInteger);
}
Now if the first field is entered correctly then the output is as wanted, false is returned. However if the first field is entered correctly and the rest are blank then the page refreshes, however it should be returning false because I check if the question and option fields are blank. Why is this happening?