我有一些表單驗證代碼似乎沒有正常工作,我無法弄清楚爲什麼。表單驗證不起作用
function isTextFieldEmpty(textField){
//return true or false depending on whether or not there is any text in the field
console.log("Checking to see if the current field is empty...");
var val = textField.value; //val is the text from the current field
console.log("The current value of 'val' is: " + val);
if(val.length < 1){
return true;
}else{
return false;
}
}
我得到的錯誤是:「遺漏的類型錯誤:無法讀取屬性未定義的‘長度’」。果然,我的控制檯日誌說'val'的值是undefined。
我敢肯定我錯過了一些東西,但我仍然在學習JS,不知道它是什麼。有什麼建議麼?
編輯:這是我傳遞什麼功能:
var uName = document.getElementById("unionname");
var noUnionName = isTextFieldEmpty(uName);
「unionname」是,我試圖驗證texfield的ID。以下是相關的HTML:
<div class="formBox">
<label for="unionname">Union Name</label>
<input type="text" name="unionname" id="unionname" value="" class="wide"/>
<span class="feedback good">Sample good message</span>
</div>
似乎好的...你確定你傳遞的參數實際上包含一個文本字段嗎? – Andrea 2011-02-25 18:12:24
@Andrea它似乎不是......嗯。這不好。 – GnomeSlice 2011-02-25 18:19:03
你傳遞給你的'isTextFieldEmpty()'函數是什麼? – drudge 2011-02-25 18:24:36