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; } }