Webmaster Forum

Go Back   Webmaster Forum > Web Development > Coding Forum

Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more.


Reply
 
Thread Tools Display Modes
Share |
  #1  
Old 05-20-2011, 05:00 AM
bidoerr bidoerr is offline
Junior Member
 
Join Date: 05-20-11
Posts: 3
iTrader: 0 / 0%
Please Help with javascript validation!

Hi!
I need help with javascript client-side validation. The problem is that validation works only on last question and I can't understand why it doesn't work on other questions....

HTML 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="http://...thanks.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>
My javascript validation
PHP 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=0i<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=0i<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=0i<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=0i<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=0i<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=0i<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;

Please help me!! It is very important!

Last edited by snakeair; 05-20-2011 at 07:09 AM.
 
Reply With Quote

Advertisement

Advertisement

  #2  
Old 05-20-2011, 04:18 PM
bidoerr bidoerr is offline
Junior Member
 
Join Date: 05-20-11
Posts: 3
iTrader: 0 / 0%
I understood that I only need one checkForm function and combine what I have. But I don't know how to do that, please help someone!!
 
Reply With Quote
  #3  
Old 05-21-2011, 02:59 PM
kos's Avatar
kos kos is offline
Coding Tiger
 
Join Date: 04-13-07
Location: .ro
Posts: 4,029
iTrader: 3 / 100%
usually, developers charge for this kind of scripts, so I'm not sure if anyone here would like or have time to do it for free...
 
Reply With Quote
  #4  
Old 05-23-2011, 01:09 AM
nemrod's Avatar
nemrod nemrod is offline
v7n Mentor
 
Join Date: 12-21-10
Location: Sweden
Posts: 463
iTrader: 0 / 0%
bidoerr: just like you said you need only one checkform() - combine the code from all of them in the first one and try your luck.
 
Reply With Quote
  #5  
Old 05-23-2011, 06:39 AM
Hades's Avatar
Hades Hades is offline
Senior Web Developer
 
Join Date: 01-23-07
Location: Argentina
Posts: 2,273
iTrader: 0 / 0%
Have you tried the JQuery validation plugin? check it out: http://bassistance.de/jquery-plugins...in-validation/

It's very easy to use and your code will look a lot cleaner.
 
Reply With Quote
  #6  
Old 05-25-2011, 04:03 AM
bidoerr bidoerr is offline
Junior Member
 
Join Date: 05-20-11
Posts: 3
iTrader: 0 / 0%
Thank you everybody for the answers!!

I already did it : )
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help on javascript validation stan1 Coding Forum 7 01-03-2011 10:45 AM
Javascript or PHP validation? ATLien Coding Forum 23 04-20-2007 02:53 PM
Javascript Number Validation imaginemn Coding Forum 1 05-18-2006 06:57 PM
Javascript Form Validation Help ATLien Coding Forum 9 03-13-2006 01:36 PM
Validation Help / Javascript rodneyj Coding Forum 0 06-23-2004 10:16 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


All times are GMT -7. The time now is 06:51 AM.
Powered by vBulletin
Copyright 2000-2014 Jelsoft Enterprises Limited.
Copyright © 2003 - 2018 VIX-WomensForum LLC