Form表單資料檢查,防止直接送出

Example1:
<form name="theForm" id="theForm" method="post" action="do.php">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name" />
  <input type="button" name="send" id="send" value="送出" onClick="checkForm();" />
</form>

function checkForm()
{
    var frm = document.forms["theForm"];
    if(frm.name.value == "")
    {
        alert("Error:name is null");
    }else{
        frm.submit();
    }
}
Example2:
<form name="theForm" id="theForm" method="post" action="do.php" onSubmit="return checkForm(this);">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name" />
  <input type="submit" name="send" id="send" value="送出" />
</form>

function checkForm()
{
    var frm = document.forms["theForm"];
    if(frm.name.value == "")
    {
        alert("Error:name is null");
        return false;
    }else{
        return true;
    }
}
本篇發表於 Javascript。將永久鏈結加入書籤。

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>