0
我正在學習JavaScript,我試圖做一個簡單的窗體驗證。 我把onblur事件放在一個表單上,它不起作用,但如果事件放在輸入本身上,它確實有效。如果有人能幫助我解決這個問題,那將會很棒。如何使此驗證工作?
function checkInfo() {
var name = document.getElementById('name').value,
email = document.getElementById('email').value;
if(name === '' || email === '') {
console.log('validate');
return false;
}
else {
return true;
}
};
<form method="post" name="myForm" id="my-form" onblur="checkInfo();">
<div>
<input type="text" id="name" name="name" placeholder="Name (Required)" required>
</div>
<div>
<input type="email" id="email" name="email" placeholder="Email (Required)" required>
</div>
<div>
<input type="text" id="subj" placeholder="Subject">
</div>
<div>
<textarea cols="30" rows="10" placeholder="Describe your project in detail..."> </textarea>
</div>
</form>