Wrox Programmer Forums
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 6th, 2006, 10:12 AM
Authorized User
 
Join Date: Aug 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validation Script

Hi Guys

Im fairly inexperienced with JavaScript and was wondering if anyone could give me a helping hand with a relatively simple validation script im trying to make.

Basically i am making an online survey with lots and lots of radio buttons on multiple pages, rather than validating each set of radio buttons line by line i tried to create a looping script which would check each radio button in the entire form and tell the user which questions have been unanswered.

On the first page there are 16 questions so i was hoping it would save me some time! Heres the code i have so far, needless to say, it isnt working!

function Validate(){

for (var x = 1; x <= 16; x++) // there are 16 questions
{
    for (counter = 0; counter <=2; counter++) // 3 possible answers
    {
       //each radio button starts with a 'q'
       if (page1.q[x][counter].checked)
       {
          return (true);
       }
       else
       {
          msg+="You have not answered question "[x]".\n";
          return (false);
        }
    }
    return (false);
}
}
Any help would be great, cheers in advance!

 
Old December 6th, 2006, 10:28 AM
Friend of Wrox
 
Join Date: Aug 2006
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post
Default

hi
tell me where u declared variable Counter and msg

thanks......
 
Old December 6th, 2006, 10:55 AM
Authorized User
 
Join Date: Aug 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I thought it was enough to declare counter in the for loop?
And the msg bit isnt quite finished yet, just need to echo it out.
I imagine the problem is the q[x] bit, i really wanted to just drop the variable x at the end of q, ie q1, q2, q3 etc but am not sure how u do that in javascript.

 
Old December 6th, 2006, 11:12 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You need someway of grouping the radio buttons by question, they should have the same name for each question so maybe you mean:
question one has radio buttons all named Q_1
question two has radio buttons all named Q_2...
If so then you need to use getElementsByName:
Code:
function getCheckedControl(groupName)
{
  var col = document.getElementsByName(groupName);
  for (var i = 0; i < col.length; i++)
  {
    if (col[i].checked) return col[i];
  }
  return null;
}

function validateRadioGroups(namePrefix, questionCount)
{
  var unansweredQuestions = "";
  for (var i = 1; i < questionCount + 1; i++)
  {
    var isAnswered = (getCheckedControl(namePrefix + i) != null);
    if (!isAnswered)
    {
      unansweredQuestions += ", " + i;  
    }
  }
  if (unansweredQuestions.length != 0)
  {
    unansweredQuestions = unansweredQuestions.substr(2)
    alert("You failed to answer question(s): " + unansweredQuestions);
  }
}

validateRadioGroups("Q_", 16);
Untested...

--

Joe (Microsoft MVP - XML)
 
Old December 6th, 2006, 04:22 PM
Authorized User
 
Join Date: Aug 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Joe

I appreciate the help, but i cant understand why my code isn't working, ive been looking at it for ages, my html doesnt even seem to register using the function i made! Its not going great!

function Do_Validate(page1)
{
    var proceed = "false";
    for (var x = 1; x <= 16; x++)
    {
        for (var counter = 0; counter <= 2; counter++)
        {
            var q = "q";
            var question = q.concat(x);
            if (page1.question[counter].checked)
            {
                proceed = "true";
            }
            else
            {
                proceed = "false";
            }
        }
    }

    if (proceed != "true")
    {
        //var msg+="You have not answered question \n";
        //echo(msg);
        return false;
    }
    else
    {
        page1.submit();
    }

}
Thats the latest javascript and the source code can be found at
http://www.williamcarson.co.uk/Evalu...orm/form1.html

If anyone can cast an eye and see why its not working i would be so so grateful. Cheers.

 
Old December 23rd, 2006, 02:29 AM
Registered User
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi Teesider,

The code you have written is excellent except this line
  if (page1.question[counter].checked)

it means you are hard coding a string

but it has to be dynamic

i tried
  var str="document."+page1;
            str+=question+"["+counter+"]";
            str+=".checked";
if (str)
{
}
[u]
even in this there is a problem!! </u>

the str is a string .but we need a boolean object to get the result from the page

if we can convert the string into a boolean then it wil be done.

I was googling a bit for that but i dont know exactly how to do it.
if you can ... tell me also
Thanks
Mohan
 
Old December 28th, 2006, 07:56 PM
Registered User
 
Join Date: Nov 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It looks like you already have the boolean in your code, you just need to return it at the end and remove the quotes from the values.

function Do_Validate(page1)
{
    var proceed = false;
    for (var x = 1; x <= 16; x++)
    {
        for (var counter = 0; counter <= 2; counter++)
        {
            var q = "q";
            var question = q.concat(x);
            if (page1.question[counter].checked)
            {
                proceed = true;
            }
            else
            {
                proceed = false;
            }
        }
    }

    if (proceed != true)
    {
        //var msg+="You have not answered question \n";
        //echo(msg);
        return false;
    }
    else
    {
        page1.submit();
    }
  return proceed;
}

Does this work for you?

 
Old January 5th, 2007, 12:22 PM
Registered User
 
Join Date: Nov 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If i put a statement like this then iam getting the result as "[String]"

but at the same place if i put it as

alert(typeof(page1.q1[1].checked))

i get the result as "[boolean]"

some where the value changes to string.. may be since we are using concat function... if we somehow convert the string in to a boolean variable and execute it .. it should work fine

function Do_Validate(page1)
{
    var proceed = false;
    for (var x = 1; x <= 16; x++)
    {
        for (var counter = 0; counter <= 2; counter++)
        {
            var q = "q";
            var question = q.concat(x);
      alert(typeof(page1.question[counter].checked)); if (page1.question[counter].checked)
            {
                proceed = true;
            }
            else
            {
                proceed = false;
            }
        }
    }

    if (proceed != true)
    {
        //var msg+="You have not answered question \n";
        //echo(msg);
        return false;
    }
    else
    {
        page1.submit();
    }
  return proceed;
}

Many Thanks,
Mohan





Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Validation using Validation Framework kalyangvd Struts 1 January 2nd, 2008 06:53 AM
Java Script Validation ricespn Classic ASP Basics 2 November 16th, 2006 02:05 PM
Call and run CGI script from a PHP script ... how? dbruins BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 June 10th, 2003 03:09 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.