Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript form validation

    I have problem with my javascript form validation... Validation works only on last question and I can't understand why it doesn't work on other questions..

    Html code :
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head>
    <script type="text/javascript" src="script.js"></script>
    
    
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Questionaire</title>
    <link href="oneColLiqCtrHdr19.css" rel="stylesheet" type="text/css" />
    
    <style type="text/css">
    <!--
    body {
    	background-color: #FFF;
    	background-image: url(images/aa.jpg);
    }
    -->
    </style></head>
    
    <body class="oneColLiqCtrHdr">
    <div id="container">
    <div id="header">
        <h1>&nbsp;</h1></div>
    
        
          <!-- forms -->
          
          
          
      <form action="http://projects.knord.dk/interaction/saveforminfo.aspx" method="post" onsubmit="return checkform(this);" >
          
      <input type="hidden" name="surveyid" value="knjuku2" />
      <input type="hidden" name="usecookie" value="true" />
      <input type="hidden" name="landingpage" value="h---.html" />  
      
    <p align="center"><b>This survey was made for Business Academy Copenhagen North project not for FDIM.</b></p>
       <p>
    
     <b>Please enter your personal information</b>
      
        </p>
       <p><b>Your Occupation:</b>
         <input type="text" name="occupation" />
        </p>
       <p><b>Age Group:</b> 
         <select name="age">
           <option value="under 18">less then 18</option>
           <option value="18-25">18-25</option>
           <option value="26-35">26-35</option>
           <option value="over 35">more then 35</option>
         </select>
         
        </p>
       <p><b>Status:</b> 
         <select name="status">
           <option value="single">Single</option>
           <option value="single with children">Single with children</option>
           <option value="in relationship">In relationship</option>
           <option value="married">Married</option>
           <option value="divorced">Divorced</option>
         </select>
        </p>
       <p><b>Gender:</b>
         <input type="radio" name="gender" value="male" /> Male
         <input type="radio" name="gender" value="female" /> Female
         
        </p>
       <p><b>Do you have a job?</b> 
         <input type="radio" name="job" value="yes" />Yes
         <input type="radio" name="job" value="no" />No  
         
       </p>
       <p>&nbsp;</p>
          <p>
            <!-- 1 vopros -->
    </p>
          <p><b>1. Why do You use online social media?</b>
            
            
            </p>
        <p>
          <input type="radio" name="bla" value="To chat with friends" /> 
          To chat with friends<br></br>
          <input type="radio" name="bla" value="To read news" /> To read news<br></br>
          <input type="radio" name="bla" value="To write blogs" /> To write blogs<br></br>
          <input type="radio" name="bla" value="Other" /> Other<br></br>
          
        </p>
        <p>
          <!-- 2 vopros -->
              
          <b>2. What online social media do You use more often?</b>
        </p>
           
        <p>&nbsp; </p>
        <p>
          <select name="more">
                
                <option value="MySpace">MySpace</option>
                <option value="Twitter">Twitter</option>
                <option value="Facebook">Facebook</option>
                <option value="LinkedIn">LinkedIn</option>
          </select>   
        </p>
        <p>&nbsp;</p>       
            
            <!-- 3 vopros -->
                                 
              <b>3. When do You use it?</b>
        <p>
            <input type="radio" name="use" value="1" /> At home<br></br>
            <input type="radio" name="use" value="2" /> At work<br></br>
            <input type="radio" name="use" value="3" /> At school<br></br>
            <input type="radio" name="iuse" value="4" /> In the bus/train<br></br>
            <input type="radio" name="use" value="5" /> Other<br></br>
           
        </p>
            <p><b>4. How much hours approximately do You spend on social media?</b>
            </p>
              <select name="hours">
               
                <option value="less than 2 hours">less than 2 hours a day</option>
                <option value="2-4 hours">2-4 hours a day</option>
                <option value="4-8 hours">4-8 hours a day</option>
                <option value="8-12 hours">8-12 hours a day</option>
                <option value="more than 12 hours">more than 12 hours a day</option>
              </select>   
              
          
        <p>&nbsp;</p>
             
                
                <p>
                <!-- 5 vopros -->
                
                <b>5. Do You use social media often?</b></p>
                <p>
               
                <input type="radio" name="often" value="Yes" /> 
                Yes
            <input type="radio" name="often" value="No" /> No
              
                
                <!-- 6 vopros -->
                </p>
                <p><b>6. Social Media website to which You usually upload</b></p>
        <p>
              <input type="radio" name="upload" value="Youtube" /> Youtube<br></br>
            <input type="radio" name="upload" value="Flickr" /> Flickr<br></br>
            <input type="radio" name="upload" value="Photobucket" /> Photobucket<br></br>
            <input type="radio" name="upload" value="Slideshare" /> Slideshare<br></br>
            <input type="radio" name="upload" value="Other" /> Other  
        </p>
           
              <!-- 7 vopros -->
              
            <b>7. Where do You chat with Your friends</b>
        
        <select name="chatting">
            
          <option value="Msn">Msn</option>
            <option value="Skype">Skype</option>
            <option value="Yahoo!">Yahoo!</option>
            <option value="Other">Other</option>
        </select>
        <p>&nbsp;</p>
       
        
          
          <!-- 8 vopros -->
     
      <b>8. You use social media for</b>    
        
          
          <select name="using">
            
            <option value="Personal reasons">Personal reasons</option>
            <option value="Professional reasons">Professional reasons</option>
            <option value="Don't use it">Don't use it</option>  
          </select>
          
        <p>&nbsp;</p>
     
      
      
        <p>
          <!-- 9 vopros -->
          
          <b>9. You prefer to obtain the information You need at</b>  
          
          
          <select name="information">
            
            <option value="Yahoo!">Yahoo!</option>
            <option value="Google">Google</option>
            <option value="Wikipedia">Wikipedia</option> 
            <option value="Ask">Ask</option> 
            <option value="Other">Other</option>  
          </select>
        </p>
        
          
          
          <!-- 10 vopros -->
          
          <b>10. What do You prefer to do being online</b>?
        
          
          <select id="drop" name="online">
            <option value="Read blogs">Read blogs</option>
            <option value="Chat with friends">Chat with friends</option>
            <option value="Watch videos">Watch videos</option> 
            <option value="Read news">Read news</option> 
            <option value="Other">Other</option>  
          </select>
        
        
        <!-- konec voprosov -->
        
        <!-- button-submit -->
        <p align="center">
      <input type="submit" value="Submit" />
      </p>
      </form>
    <!-- end #container -->
    </div>
    </body>
    </html>
    Javascript validation
    Code:
    //Javascript Document
    
    function checkform ( form )
    {
    
      if (form.occupation.value == "") {
        alert( "Please enter your occupation." );
        form.occupation.focus();
        return false ;
      }
      return true ;
    }
    
    
    function checkform(f) {
      for (var i=0; i<f.elements("gender").length;
        i++) {
        var radio = f.elements("gender")[i];
        if (radio.checked) {
          return true;
        }
      }
      // no checked radio button found
      window.alert("Choose your gender!");
      f.elements("gender")[0].focus();
      return false;
    }
    
    function checkform(f) {
      for (var i=0; i<f.elements("job").length;
        i++) {
        var radio = f.elements("job")[i];
        if (radio.checked) {
          return true;
        }
      }
      // no checked radio button found
      window.alert("Choose if you have a job!");
      f.elements("job")[0].focus();
      return false;
    }
    
    function checkform(f) {
      for (var i=0; i<f.elements("bla").length;
        i++) {
        var radio = f.elements("bla")[i];
        if (radio.checked) {
          return true;
        }
      }
      // no checked radio button found
      window.alert("Answer question number 1");
      f.elements("bla")[0].focus();
      return false;
    }
    
    function checkform(f) {
      for (var i=0; i<f.elements("use").length;
        i++) {
        var radio = f.elements("use")[i];
        if (radio.checked) {
          return true;
        }
      }
      // no checked radio button found
      window.alert("Answer question number 3!");
      f.elements("use")[0].focus();
      return false;
    }
    
    function checkform(f) {
      for (var i=0; i<f.elements("often").length;
        i++) {
        var radio = f.elements("often")[i];
        if (radio.checked) {
          return true;
        }
      }
      // no checked radio button found
      window.alert("Answer question number 5!");
      f.elements("often")[0].focus();
      return false;
    }
    
    function checkform(f) {
      for (var i=0; i<f.elements("upload").length;
        i++) {
        var radio = f.elements("upload")[i];
        if (radio.checked) {
          return true;
        }
      }
      // no checked radio button found
      window.alert("Answer question number 6!");
      f.elements("upload")[0].focus();
      return false;
    }
    Where is my mistake? And how to solve it to make javascript validation work properly?
    Last edited by Kor; May 20th, 2011 at 08:37 PM. Reason: wrap the code [code][/code] an remove the extra font size. We are not blind

  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    19,551
    Thanks
    221
    Thanked 2,712 Times in 2,686 Posts
    You have numerous functions with the same name - checkform(). Only the last one will execute. They must have different names, or the validations be included all togther in one function.

    Note if the script encounters return true or return false it will do just that and the script will terminate.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  3. #3
    Master Coder Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You can not have several function with the same name. You must group all your expressions in a single function. Or call different named functions one from another, if needed.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  4. #4
    New to the CF scene
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How to group them?


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •