Basically, what I am trying to do is alert the same message if one of the seven text boxes in my form is empty. I have been playing around with it a lot, but now I am starting to feel exasperated; help! Sometimes I get a function validepiste() is not defined error and sometimes I don’t get an error, but there is an alert even if all the boxes have something in them. I think the error might be due to an incorrect use of the || (or) operator, or the length bit. Here is the function (which is within the head tags):
<script type="text/javascript">
function validatepiste()
{
// all form fields must be filled in
var a= document.piste.nom.length;
var b= document.piste.email.length;
var c= document.piste.numtel.length;
var d= document.piste.adresse.length;
var e= document.piste.ville.length;
var f= document.piste.length;
var g= document.piste.travaux.length;
if (a||b||c||d||e||f||g==0){
alert("Vous devez remplir toutes les cases.");
return false;
}
return true
}
</script>
And the call to that function which is in the html:
<input type="submit" name="Submit" value="Envoyer" onClick="return validatepiste()"/>
Thank you!
There are two issues with your code that I can see.
First Problem
It appears you’re trying to access a
lengthproperty directly on a form element (which doesn’t exist). What you really want to do is access thelengthof a form value. So instead of this:Do this:
I didn’t see this at first but @Napster mentioned it in his answer. So thank him.
Second Problem
You’re if statement has some bad logic. Try the following instead.
Your original expression was asking the following…
If
ais truthy,or
bis truthy,or
cis truthy,or
dis truthy,or
eis truthy,or
fis truthy,or
gis equal to0then show the alert.
The only scenario which would not have triggered the alert using your existing code would be if all the fields were blank except for
g.You can read an article about what truthy means if you like.