0

My java script validation is not working for my form. If ionsubmit="return(validate());"ut alert("fswefefew"); at the top of the tage I get the alert but the if I take it out it just sends my form with no validation.

<form name="Contact" onSubmit="return validate()" METHOD="POST" ACTION="reply.php">


<div class="Row">
   <div class="Lable">Full Name:</div> <!--End of Lable-->
   <div class="input">
<input type="text" id="fullname" class="detail" name="fullname" placeholder="Full Name" />
    </div> <!--End input-->
</div> <!--End row-->


   <br />
   <div class="Row">
   <div class="Lable">Email Address:</div> <!--End of Lable-->
   <div class="input">
<input type="text" id="emailaddress" class="detail" name="emailaddress"    placeholder="Email Address" />
</div> <!--End input-->
</div> <!--End row-->

       <br />
        <div class="Row">
  <div class="Lable">Your Message:</div> <!--End of Lable-->
      <div class="input">
     <textarea id="comment" name="comment" class="mess" placeholder="Your Message">   </textarea>
</div> <!--End input-->
</div> <!--End row--> 

   <br />
<div class="Row">
<div class="Lable">Select your gender:</div> <!--End of Lable-->
   <div class="input">   
  <input type="radio" name="gender" value="male"  id="gender_male" />
   <label for="gender_male"/>Male
<input type="radio" name="gender" value="female" id="gender_female" />
   <label for="gender_female" />Female
   </div> <!--End input-->
</div> <!--End row--> 




<br />
  <div class="Row">
  <div class="Lable">Please select your age:</div>
  <!--End of Lable-->
   <input type="radio" name="age" id="range1" value="18-25" />
  18-25 
  <input type="radio" name="age" id="range2" value="26-33" />26-33 
  <input type="radio" name="age" id="range3" value="24-40" />34-40 
  <input type="radio" name="age" id="range4" value="40+" />40+<br />
 </div><!--Row-->

  <br />
  <div class="Row">
<div class="Lable">Select 3 products that you are interested in hearing about:</div> 
<!--End of Lable-->
<input type="checkbox" name="Interested[]" id="protien" value="protien" />Protien
<input type="checkbox" name="Interested[]" id="creatine" value="creatine" />Creatine<br>
<input type="checkbox" name="Interested[]"  id="bcaa" value="bcaa" />BCAA
<input type="checkbox" name="Interested[]" id="power drinks" value="powerdrinks" />Power Drinks<br />
</div><!--Row-->





<br />
<div class="Row">
<div class="Lable">Where did you hear about us?</div> <!--End of Lable-->
<select>
<option value="" />Nothing
<option value="Google Search" selected="selected" id="heard[]" name="heard[]" /> Search
<option value="Word of mouth" name="heard[]" id="heard[]" /> Word of mouth 
<option value="Newspaper" name="heard[]" id="heard[]" /> Newspaper
<option value="Magazine" name="heard[]" id="heard[]" /> Magazine
</select>
 </div><!--Row-->



 <br />
 <div class="submit">
 <input type="submit" id="send" Name="send" value="Send" />
  </div><!--End of submit-->

 <div class="Clear">
  <input type="reset" id="clear" Name="Clear" value="Clear" />
 </div>





</form>

And this is my javascript

 <script type="text/javascript">

 <!--
 function validate()
  {
alert("Please enter your name");
  //Check to see if name has been entered - the form is named fb_form
  if (Contact.fullname.value == "")
  {
     alert("Please enter your name");
     return false;
     } 
   else
    {
    alert("Please enter your email correctly");
    }

    //This section checks there is an '@' in the email address string
    if (Contact.emailaddress.value.indexOf('@') < 1 )
      {  
      alert("Please enter your email correctly");
      return false;
    }


     if (Contact.comment.value =="" )
       {  
      alert("Please enter a message");
      return false;
     }



     //This next section sees if one of the checkboxes has been checked 
    if (((Contact.gender_male.checked) || (Contact.gender_female.checked)) == false)
   {
      alert("Please tell us your gender");
      return false;
    }

      if (((Contact.range1.checked) || (Contact.range2.checked)  ||       (Contact.range3.checked)  || (Contact.range4.checked)) == false)
     {
       alert("Please tell us your age range");
       return false;
    }
     //All is OK, then return true

        if (Contact.protien.checked || Contact.creatine.checked  || Contact.bcaa.checked  || Contact.powerdrinks.checked)
     {
       alert("good");

      }
     else
     {
       alert("bad");
      return false; 
    }



      if (Contact.heard.selectedIndex < 1 )
      {
       alert("Please rate our site");
      return false;
     } 

     return true;
   }

  //-->
   </script>
7
  • is it return(validate()) or return validate()? Commented Apr 1, 2014 at 10:40
  • Try correcting return validate()) in <form name="Contact" onSubmit="return validate())" METHOD="POST" ACTION="reply.php"> Commented Apr 1, 2014 at 10:41
  • Sorry it's return validate() I tried adding that after reading something and didn't take it out before I posted still got the issues either way Commented Apr 1, 2014 at 10:45
  • Are you sure you are using the correct DOM selector? Commented Apr 1, 2014 at 10:49
  • @iamslleepy yes im sure. If I put alert on the first line I get the error on the web page but it ignores the rest of the code. Commented Apr 1, 2014 at 10:51

1 Answer 1

1

There is a problem in your java script code you have missed document in your syntax

document.Form.Name.value

For your checkbox validations try this code:

 var options = document.getElementsByName("Interested[]");
if(options[0].checked==false && options[1].checked==false && options[2].checked==false) {
        alert('Please check at least one of the options.');
        return false;
    }
    return true; 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you iv got all .value fields working but still having problems with.checked. any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.