gorillagogo
05-03-2003, 12:23 AM
I'm a newbie and am having difficulty with a standard form validation. I want to ensure that the 'name' and 'comment' fields in a form are not left blank. The script runs fine, but when I leave a field blank, the action doesn't cancel. Here's the code snippet:
<script language="javascript">
<!--
function checkForm(form){
if(form.user.value != null && form.user.value != ""){
if(form.commenttext.value != null && form.commenttext.value != ""){
return true
}
}
alert("Name and comment are required fields.")
return false
}
-->
...
<form action="comments.php" onsubmit="checkForm(this)">
When I leave a required field blank, I get the message box telling me that the fields are required, but after that the form's action isn't cancelled. Any thoughts as to what I'm doing wrong?
<script language="javascript">
<!--
function checkForm(form){
if(form.user.value != null && form.user.value != ""){
if(form.commenttext.value != null && form.commenttext.value != ""){
return true
}
}
alert("Name and comment are required fields.")
return false
}
-->
...
<form action="comments.php" onsubmit="checkForm(this)">
When I leave a required field blank, I get the message box telling me that the fields are required, but after that the form's action isn't cancelled. Any thoughts as to what I'm doing wrong?