Solved

verify form

Posted on 1999-01-11
15
776 Views
Last Modified: 2012-05-04
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
Comment
Question by:marijka
  • 6
  • 6
  • 2
  • +1
15 Comments
 

Expert Comment

by:aseem072898
ID: 1276708
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
 

Author Comment

by:marijka
ID: 1276709
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
 
LVL 6

Expert Comment

by:PBall
ID: 1276710
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
 
LVL 6

Expert Comment

by:PBall
ID: 1276711
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
 

Author Comment

by:marijka
ID: 1276712
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
 
LVL 6

Expert Comment

by:PBall
ID: 1276713
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
 

Expert Comment

by:MagicJ
ID: 1276714
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:marijka
ID: 1276715
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
 
LVL 6

Expert Comment

by:PBall
ID: 1276716
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
 

Expert Comment

by:MagicJ
ID: 1276717
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
 
LVL 6

Expert Comment

by:PBall
ID: 1276718
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
 

Author Comment

by:marijka
ID: 1276719
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
 

Author Comment

by:marijka
ID: 1276720
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
 
LVL 6

Accepted Solution

by:
PBall earned 100 total points
ID: 1276721
OK.

I've come to claim my points :)

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


0
 

Author Comment

by:marijka
ID: 1276722
Thank you.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now