2016-05-04 25 views
0

驗證表單字段的Javascript函數FormQuote_Validator應返回「TRUE」,並在所有三個輸入字段均未提交任何數字時提醒消息,否則應返回「FALSE」。如何使用「OR」運算符進行表單字段驗證?

下面是HTML代碼:

<form id="gform_1" enctype="multipart/form-data" method="post" action=""> 
<div> 
    <li id="field_1_25"> 
    <label for="input_1_25">20 Amps</label> 
    <input type="number" tabindex="22" class="small" value="" step="any" id="input_1_25" name="input_25"> 
    </li> 

    <li id="field_1_26"> 
    <label for="input_1_26" class="gfield_label">30 Amps</label> 
    <input type="number" tabindex="23" class="small" value="" step="any" id="input_1_26" name="input_26"> 
    </li> 

    <li id="field_1_27"> 
    <label for="input_1_27">40 Amps</label> 
    <input type="number" tabindex="24" class="small" value="" step="any" id="input_1_27" name="input_27"> 
    </li> 
</div> 

<button onclick="FormQuote_Validator(gform_1)" type="button">Submit</button> 
</form> 

這是使用Javascript:

function FormQuote_Validator(Form){ 
    if ((Form.input_25.value == "") || (Form.input_26.value == "") || (Form.input_27.value == "")){ 
    alert("Please input the size in Amps."); 
    Form.input_1_25.focus(); 
    return (false); 
    } 
} 

由於種種原因FormQuote_Validator函數返回「TRUE」,即使一個或兩個輸入字段以數值提交。腳本代碼有問題嗎?任何幫助,將不勝感激!

+0

什麼不起作用?控制檯中有任何錯誤?請詳細說明_「不工作!」_ – Rayon

+0

似乎爲我工作,請參閱:https://jsfiddle.net/cdLyczr7/ –

回答

0

因爲||意味着OR,這意味着每個輸入必須填寫。

我已經把你的代碼在codepen,和我看到正確的行爲。

如果你想要顯示警告框,如果沒有填寫一個單一的輸入,那麼你應該使用AND(&&)。

+0

這就是我所需要的 - >「每個輸入將不得填寫」。只有當所有三個輸入字段都沒有提交任何值時,它纔會返回「TRUE」。 – Alex

+0

這就是發生了什麼 –

+0

不,它沒有發生。只需在第一個輸入字段中輸入任意數字並點擊提交。爲什麼它仍然返回「TRUE」? – Alex