There are two ways to form validation:
First, onsubmit verification:
<script language= "JavaScript" > function CheckPost () { if (addform.user.value == "") { alert ("Please fill in the user name!") "); addform.username.focus (); return false; } if (addform.title.value.length < 5) { alert ("title cannot be less than 5 characters!") "); addform.title.focus (); return false; } return true; } </script><form action= "test.php" method= "post" name= "AddForm" Onsubmit= "Return checkpost ();" > <p> User: <input type= "text" size= "ten" name= "user" maxlength= "/></p> <p>" title: <input type= "Text" name= "title" maxlength= "/></p> " <p> content: <textarea name= "Content" rows= "8" cols= "+" ></textarea></p> <p><input type= "Submit" name= "Submit" Onclick= "Sendform ();" value= "Leave a Message"/></p> </form><?php if ($_ post[' Submit ']) { echo < Script language=\ "javascript\" >alert (' Submit success! '); History.go ( -1);</script> "; } ? >
Two, onclick authentication:
<script language= "JavaScript" > function SendForm () { if (Checkpost ()) { document.addform.submit (); } } function CheckPost () { if (addform.user.value == "") { alert ("Please fill in the user name!") "); addform.username.focus (); return false; } if (addForm.title.value.length &NBSP;<&NBSP;5) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBsp; alert ("title cannot be less than 5 characters!") "); addform.title.focus (); return false; } return true; } </script>
JS
for form validation