Click to See Complete Forum and Search --> : form validation


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?

khalidali63
05-03-2003, 07:02 AM
Here you go..you had a tiny bit of logical problem..

function checkForm(form){
if(form.user.value != "" && form.commenttext.value != ""){
return true
}else{
alert("Name and comment are required fields.")
return false
}
}

:D

khalidali63
05-03-2003, 07:30 AM
:D

Thanks Dave...overlooked the form action part...

gorillagogo
05-03-2003, 09:12 AM
thanks. changing the onsubmit did the trick. :) just for my own knowledge, though, what's the advantage to changing the script? I realize my nested ifs were clumsy, but i'm unfamiliar with this line in your enhancement:

if (/^\s*$/.test(user.value) {

what does this line do?