如何验证空文本输入并停止显示NaN?

时间:2012-12-05 22:56:07

标签: jquery html

我正在使用以下HTML代码来显示表格:

<table border='1' id='markstbl'>
    <thead>
    <tr>
    <th class='questionth'>Question No.</th>
    <th class='answermarksth'>Marks per Answer</th>
    </tr>
    </thead>
    <tbody>

    <tr class="questiontd">
    <td class="questionnumtd" name="numQuestion" rowspan="2">1 <input type="hidden" name="q1_ans_org" class="q1_ans_org" value="5"><input type="hidden" name="q1_ans" class="q1_ans"></td>
    <td class="answermarkstd">
    <input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
    </td>
    </tr>

    <tr class="questiontd">
    <td class="answermarkstd">
    <input class="individualMarks q1_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
    </td>
    </tr>

    <tr class="questiontd">
    <td class="questionnumtd" name="numQuestion" rowspan="1">2 <input type="hidden" name="q2_ans_org" class="q2_ans_org" value="5"><input type="hidden" name="q2_ans" class="q2_ans"></td>
    <td class="answermarkstd">
    <input class="individualMarks q2_mark_0"  q_group="1" name="answerMarks[]" id="individualtext" type="text" />
    </td>
    </tr>
    </tbody>
    </table>

下面是表格的样子:

Question No.     Marks Per Answer         
    1            (blank text input)       
                 (blank text input)
    2            (blank text input)       

如您所见,问题1包含2个文本输入,而问题2包含1个文本输入。

现在我想用下面的代码做的是验证任何空文本输入。

function validation() {

    var alertValidation = "";
    var _qid = "";
    var _msg = "";

    $("[class*='q']").each(function(i) {  
    var questions = parseInt($("[class*=q" + i + "_qnum]").text());
    var marks = parseInt($("[class*=q" + i + "_ans_text]").text()); 
    var txtinput = $("[class*=q" + i + "_mark]").val(); 
    _qid = questions;
    _msg = "You have errors on Question Number: " + _qid + "\n";


if (txtinput == '' || txtinput == null) {
    alertValidation += "\n\u2022 You have not entered in a value for all the Indivdiaul Marks textbox\n";
}    

            if (alertValidation != "") {
                return false; //Stop the each loop 
            }

        });


    if (alertValidation != "") {
        alert(_msg + alertValidation);
        return false;
    }

    return true;
}

问题是在警报中它不会显示问号_qid,因为它显示为NaN,即使没有文本输入为空,它仍会显示一条警告,指出并非所有输入都包含价值。

我的问题是如何显示问题编号以及如何正确检查空文本输入是否正确,这样如果文本输入为空,则显示带有正确问题编号的警报,如果所有文本输入包含值,然后它不显示验证?

我想说_qid确实适用于我在validation()函数中包含下面的代码以进行另一次验证检查时,该函数正好位于检查空文本输入的if语句之下:< / p>

if(marks < '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) +  " Marks";   
}

        else if(marks > '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks +  " Marks Remaining";   
}

当针对上述验证显示警报时,它会在警报中显示问题编号。当我验证空文本输入时,它只是不显示它?是因为我可能在一个问题中有多个文本输入,所以我可能需要在代码中包含另一个foreach()循环来输入空文本?

以下是完整Validation()代码所示的内容:

function validation() {

    var alertValidation = "";
    var _qid = "";
    var _msg = "";

    $("[class*='q']").each(function(i) {  
    var questions = parseInt($("[class*=q" + i + "_qnum]").text());
    var marks = parseInt($("[class*=q" + i + "_ans_text]").text()); 
    var txtinput = $("[class*=q" + i + "_mark]").val(); 
    _qid = questions;
    _msg = "You have errors on Question Number: " + _qid + "\n";


if (txtinput == '' || txtinput == null) {
    alertValidation += "\n\u2022 You have not entered in a value for all the Indivdiaul Marks textbox\n";
}    


if(marks < '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Need To Remove " + Math.abs(marks) +  " Marks";   
}

        else if(marks > '0')
{

 alertValidation = "Your Total Marks Remaining does not equal 0 \n\n\u2022 You Have " + marks +  " Marks Remaining";   
}

            if (alertValidation != "") {
                return false; //Stop the each loop 
            }

        });


    if (alertValidation != "") {
        alert(_msg + alertValidation);
        return false;
    }

    return true;
}

以下是验证消息的示例:

标记高于0的验证:

You have errors on Question Number: 1
Your Total Marks Remaining does not equal 0 

• You Have NaN Marks Remaining

标记小于0的验证:

You have errors on Question Number: 2
Your Total Marks Remaining does not equal 0 

• You Need To Remove NaN Marks

空文字输入验证:

You have errors on Question Number: NaN

• You have not entered in a value for all the Indivdiaul Marks textbox

如您所见,警报是唯一一个不显示问号

的警报

由于

0 个答案:

没有答案