• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 798
  • Last Modified:

verify form

I'm trying to verify a form, to insure that all the questions are answered before it is submitted.  Below is some code that does not work.  I think the problem is in how I am checking on the value of the inside function.  Please help me figure out how to get this verification to work.

<script language="JavaScript">
<!--
function verify()
{
      var FormOK = true;
function IsFormComplete(study_form)
{
var x       = 0
var FormOk  = true

while ((x < document.forms[study_form].elements.length) && (FormOk))
   {
     if (document.forms[study_form].elements[x].value == '')
     {
        FormOk = false
     }
     x ++
   }
return FormOk
}

if (FormOK == true) return true;

if (FormOK == false)
{
 alert('Please enter the '+document.forms[FormName].elements[x].name +' and try again.')
        document.forms[FormName].elements[x].focus();
 return false;
}
}
// -->

</script>

<FORM METHOD=POST ACTION="cold.pl" NAME="study_form" onSubmit="return verify();">
0
marijka
Asked:
marijka
  • 6
  • 6
  • 2
  • +1
1 Solution
 
aseem072898Commented:
Use this

<HTML>
 <HEAD>

<SCRIPT LANGUAGE="Javascript">
<!--
         
function IsFormComplete(FormName)
{
var x       = 0
var FormOk  = true

while ((x < document.forms[FormName].elements.length) && (FormOk))
   {
     if (document.forms[FormName].elements[x].value == '')
     {
        alert('Please enter the '+document.forms[FormName].elements[x].name +' and try again.')
        document.forms[FormName].elements[x].focus()
        FormOk = false
     }
     x ++
   }
return FormOk
}



//-->
</script>
         
         
 </head>
<BODY>
</body>
</html>

Regards
Aseem
0
 
marijkaAuthor Commented:
This is not what I'm looking for.  This is just a part of the script that I submitted.  What I need is to have the script verify the form, alert the user if the form is not complete and then submit the form to my cgi script.  

My problem is that the alert will come up and then it will submit the form anyway, eventhough it is not complete.
0
 
PBallCommented:
function verify()
{
var FormOK = true;

//Heh!! I didn't know you could have nested function in Javascript
//That's one mistake.

function IsFormComplete(study_form)
{
var x       = 0
var FormOk  = true

while ((x < document.forms[study_form].elements.length) && (FormOk))
   {
     if (document.forms[study_form].elements[x].value == '')
     {
        FormOk = false
     }
     x ++
   }
return FormOk

//OK, when you return something, it will exit the function immediately (code below the return will not be executed.
}

if (FormOK == true) return true;

if (FormOK == false)

//this will never happen because of return in the while statement.
{
 alert('Please enter the '+document.forms[FormName].elements[x].name +' and try again.')
        document.forms[FormName].elements[x].focus();
 return false;
}
}

//again, there is no such thing as nested function in javascript.  pascal maybe :)

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
PBallCommented:
Also, althought if coded correctly, that code would work, it will only work on textarea and input type=text/password/hidden/submit/button form fields.

What about select/option, radio button, and checkboxes?

0
 
marijkaAuthor Commented:
I wasn't sure nesting functions was actually possible  ;-)

I guess I need to rethink how I'm doing this verification...

What I need is to be able to verify that for any question named "x", "x" has a value before the form is submitted.  Most of my questions take on a radio button form but they still begin with a value of null and end with an actual value and each question can only have one value, so why wouldn't the above, inside function work?

Any suggestions would be great.

Thank you.
0
 
PBallCommented:
You might want to take a look at builder.com superscripter column by charity khan.  she did something similar to this (the almighty all powerful all functioning generic enough validation routine hehe).

basically, before actually testing the value of the field, you can get its typeof and compare it and branch out to the correct validation routine.  that way, you can use a single almighty validator to do the form validation for you.

I myself still bound to the field by field validation routine, there are just too many variables out there to be taken into account such as date text field, numeric only text field, email addresses, ip address and what have you.

0
 
MagicJCommented:
Hi Marijika,

If u have radio buttons or checkboxes ur code will not work. In such cases u have to use document.forms[x].elements[y].checked, this will return a true or false based on checked or not. Then the logic u've written will have problems when first control has no value and the second has. The flag will be true and the form will get submitted. This is what I understood from the prg.Tell me if u want some code..

MagicJ
0
 
marijkaAuthor Commented:
I looked up the article by Charity Kahn, which was very helpful, (thanks PBall).  But now I have the following question:  My form has 27 questions each of which are of the radio button selection type.  Instead of having to write a seperate function for each radio button question, is there a way of creating an array of radio button questions that can all be checked by one function?    See the example code below.

<script language="JavaScript">
<!--

@button = (form.q1.length,form.q2.length....etc)

function RadioChecked(form) {
      for (i=0; i<button.array; i++) {
            if (button.array[i].checked) {
                  return true;
            }
      }
      return false;
      }

function VerifyForm(form){
      if (!RadioChecked(form)) {
            alert("Please answer all questions.");
            return false;
            }
      else {
            return true;
            }
      }
      


// -->

</script>


<FORM METHOD=POST ACTION="cold.pl" NAME="study_form" onSubmit="return VerifyForm(this);">


0
 
PBallCommented:
Maybe not really what you want to hear, but if you name your radio buttons similarly with a sequential number for each question.  you can parse that instead.

i.e.

<input type=radio name=radQ1 value='A'> She loves me
<input type=radio name=radQ1 value='B'> She don't loves me
<input type=radio name=radQ1 value='C'> I don't know

<input type=radio name=radQ2 value='A'> My pet
<input type=radio name=radQ2 value='B'> My wife
<input type=radio name=radQ2 value='C'> My mother

:

<input type=radio name=radQ27 value='A'> Hawaii
<input type=radio name=radQ27 value='B'> Bermuda
<input type=radio name=radQ1 value='C'> Tahiti

you get my drift?

Now in a script you can do something like:

function radioCheck()
{
var firstQ = 1
var lastQ = 27

for (var curQ = firstQ; curQ <= lastQ; curQ++)
{
  //dereference the button group object
  var c = eval('document.frmQuestionnaire.radQ'+curQ);

  //partial answer on the radio button group - reset to false
  var p = false;

  //parse the button group and if any is checked, then
  //assign true to p and get out of the loop.

  for (var i in c)
   if (c[i].checked=="1") {
     p = true;
     break;
   }

  //oops, no button was checked in the current button group
  //thus the function failed.
  if (!p) return false;
}

//well, since everything each button group has a checked value,
//function succeed.
return true;
}


0
 
MagicJCommented:
Can u just give a sample as How the questions are going to be? 'cause questions can be multiple choice or fill in the blanks.... so can u give  a sample so that I can help u.
0
 
PBallCommented:
oops...
some post-post error corrections

pardon my grammar, it's a bit (actually a lot) screwed up. She don't love me (oh my god-where did that come from-hehe), she doesn't love me :)

hehe the last input radio in the example should be radQ27 not radQ1.

Don't you just hate it when that happens?  You post and then re-read it and there are those minute imperfections...ugh.  My mind is thinking faster than my eyes and fingers can, hehe.
0
 
marijkaAuthor Commented:
I hate making typos, they are the worst kind of mistake.

It took me a little time to get back to you on this because I needed to see how changing the field names would effect the end importing of the data into the database structure. It does change the import process but I can work around it.

You can take a look at the finished product as it stands at http://www.fammed.wisc.edu/samplecold/samplecold.html

PBall, if you want points for helping me and you definately deserve them, just submit an answer and they are yours.  Thank you.
0
 
marijkaAuthor Commented:
I hate making typos, they are the worst kind of mistake.

It took me a little time to get back to you on this because I needed to see how changing the field names would effect the end importing of the data into the database structure. It does change the import process but I can work around it.

You can take a look at the finished product as it stands at http://www.fammed.wisc.edu/samplecold/samplecold.html

PBall, if you want points for helping me and you definately deserve them, just submit an answer and they are yours.  Thank you.
0
 
PBallCommented:
OK.

I've come to claim my points :)

The answer is in the comments if anybody is looking...


0
 
marijkaAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 6
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now