如果「名稱」輸入字段爲空,我想保留在當前頁面上。 現在,它顯示錯誤消息,並且當您單擊確定時,它仍會轉到下一頁(contactcaptcha.php)。當表單驗證爲false時,保持當前頁面
function notEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus();
return false;
}
return true;
}
<form id="action" action="contactcaptcha.php" method="post">
<fieldset>
<textarea id="message" name="Message" placeholder="What's on your mind?"></textarea>
<input id="Name" name="Name" placeholder="Enter your full name" type="text">
<input id="Email" name="Email" placeholder="Enter your email address" type="email">
<input type="submit" onclick="notEmpty(document.getElementById('Name'), 'Please enter your name')" name="submit" value="Send your message">
</fieldset>
只是想指出的是,當按鈕被點擊,這將不工作,當表單提交...因此,如果用戶按「確認」時,重點是最後一個字段,他將提交表格*,不需要點擊提交按鈕。 :) – fcalderan