質問内のテキスト入力が空の場合にアラートで検証メッセージを生成したいと考えています。たとえば、質問 1 に空白のテキスト入力が 2 つある場合、両方のテキスト入力が空白の場合、検証メッセージが表示されますYou have not entered in a value in all the Indivdiaul Marks textbox
。
ただし、問題は、たとえば、質問 1 の場合、1 つのテキスト入力が空白で、もう 1 つのテキスト入力が空白でない場合、検証メッセージが表示されないことです。質問 1 のすべてのテキスト入力が入力されているわけではないため、検証メッセージが表示されるはずです。
私の質問は、質問ごとに空白のテキスト入力がある場合、どうすれば検証メッセージを表示できるかということです。
ここにフィドルがありますので、テストできます:http://jsfiddle.net/cbyJD/87/
以下は、validation() 関数コードです。
function validation() {
// only keeping track of the final message
var alertValidation = "",
// toggle for showing only one error
showOnlyOneError = true;
$("input[data-type='qmark']").each(function(i) {
var questions = $(this).attr("data-qnum");
var marks = parseInt($("[class*=q" + (i+1) + "_ans_text]").text());
var txtinput = $(this).val();
// the message for this question
var msg = '';
if (txtinput == '') {
msg += "\n\u2022 You have not entered in a value in all the Indivdiaul Marks textbox \n";
}
if (marks < 0) {
msg += "\n\u2022 Your Total Marks Remaining does not equal 0 \n - You Need To Remove " + Math.abs(marks) + " Marks";
} else if (marks > 0) {
msg += "\n\u2022 Your Total Marks Remaining does not equal 0 \n - You Have " + marks + " Marks Remaining";
}
// if there is an error for the question, add it to the main message
if (msg.length) {
alertValidation += alertValidation.length ? '\n\n' : '';
alertValidation += "You have errors on Question Number: " + questions + "\n";
alertValidation += msg;
// stop if we only care about the first error
return !showOnlyOneError;
}
});
// show the error messages
if (alertValidation != "") {
alert(alertValidation);
return false;
}
return true;
}