簡體   English   中英

錯誤消息未顯示用於表單驗證Javascript

[英]Error messages not displaying for form validation Javascript

不太確定我做錯了什么。 但是我的兩個錯誤沒有顯示,即nPrice和nAmount。 但是,將顯示我的名字和nSmokes。 我認為這與我正在使用的if的多重條件有關。

我真的很想你的想法。

提前致謝。

function doValidate()
{

//alert("Working");

document.myForm.nPrice.value=parseFloat(document.myForm.nPrice.value);
document.myForm.nAmount.value=parseInt(document.myForm.nAmount.value);
document.myForm.nSmokes.value=parseInt(document.myForm.nSmokes.value);

//alert(document.myForm.nPrice.value);

//alert(document.myForm.nAmount.value);

//alert(document.myForm.nSmokes.value);



errorCount = 0;
errorMsg = ""

if( document.myForm.name.value == "") {

        errorMsg = errorMsg + "You need to enter a value!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 

}

if (document.myForm.nPrice.value < 0 && document.myForm.nPrice.value > 20.00) {

        errorMsg = errorMsg + "You need to enter a value between 0 and 20!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}

if (document.myForm.nAmount.value < 0 && document.myForm.nAmount.value > 40) {

        errorMsg = errorMsg + "You need to enter a value between 0 and 40!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}
if (document.myForm.nSmokes.value < 0) {

        errorMsg = errorMsg + "<br/>Either your doing a great job or you put in that you     smoked 0 today!";
        document.getElementById('errors').innerHTML= errorMsg;
        errorCount++; 
}

if (errorCount > 0) {
        return false;
} else {
        return true;
}
}

的HTML

<form name="myForm" method="post" id="myForm" onsubmit="return doValidate();">

  <table width="600" border="1" cellspacing="4" cellpadding="4">
   <tr>
     <td colspan="2" align="center"><img src="quitSmoking.jpg"  width="278"/></td>
   </tr>
   <tr>
     <td colspan="2" align="center">Quit Smoking Calculator by Your Name</td>
  </tr>
   <tr>
    <td>Your Name</td>
    <td><input type="text" name="name" id="name"  />&nbsp;</td>
   </tr> 
   <tr>
     <td>Date you Quit</td>
     <td><input name="startDate" type="text"> 
   <input type="button" value="select" onclick="displayDatePicker('startDate');">&nbsp;</td>
    </tr>
     <tr>
       <td>Price of a pack</td>
      <td><input type="text" name="nPrice" id="nPrice" value="8.00" />&nbsp;</td>
     </tr>
       <tr>
       <td>How many in a pack</td>
       <td><input type="text" name="nAmount" id="nAmount" value="20" />&nbsp;</td>
     </tr>
     <tr>
       <td>Number of cigarettes you smoked a day</td>
       <td><input type="text" name="nSmokes" id="nSmokes" value="25" />&nbsp;</td>
     </tr>
     <tr>
      <td colspan="2" align="center"><input type="submit" name="btnSubmit" id="btnSubmit" value="Calculate Savings" /></td>
    </tr>
     <tr>
       <td colspan="2" align="center"><div id="errors"></div></td>
     </tr>
     <tr>
       <td id="rowResult" colspan="2" align="center">
       <input type="hidden" name="nResult" id="nResult" value="" />
       &nbsp;</td>
     </tr>
   </table>

    </form>
document.myForm.nPrice.value < 0 && document.myForm.nPrice.value > 20.00

值如何小於0 大於20? 您正在那里尋找OR。

document.myForm.nPrice.value < 0 || document.myForm.nPrice.value > 20.00

介於0到20之間

document.myForm.nPrice.value > 0 && document.myForm.nPrice.value < 20.00

如果表達也犯了類似的錯誤。

該行將無法正常工作:

document.myForm.nSmokes.value = parseInt(document.myForm.nSmokes.value);

再次將值重新放入表單后,該值將被轉換回字符串,這基本上就是您在上面的行中所做的。 相反,您需要將解析后的值存儲在變量中,然后在驗證中使用這些變量。

var nSmokes = parseInt(document.myForm.nSmokes.value);

然后使用它像這樣:

if (nSmokes < 0) {

此外,由於使用的是&& (AND)運算符而不是|| ,因此邏輯上有錯誤。 (OR)運算符,這會更有意義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM