Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alert the user of worng answer

Status
Not open for further replies.

papamma

Programmer
Joined
May 9, 2006
Messages
1
Location
US
I am new to Javascript. I am working for a medical research firm. I have a form which has two criteria's in it. One is INCLUSION criteria(which has 10 questions with two(yes/no) checkboxes and the other is EXCLUSION criteria(which has 20 questions with two(yes/no) checkboxes) and I have a save button at the bottom.
When the user answers these questions and clicks the save button, I need to check the values of all the 30 questions and alert the user(if the answers are wrong) with the msg "All Inclusions should be answered YES and exclusions should be answered NO" along with the questions that were answered wrong.
Can someone please help me with this. I need this urgently.
Thanks a lot in advance.
 
Show us your form and we can help you a lot easier.
First, you should use radio buttons rather than checkboxes since you only want to allow Yes OR No but not both.

You want to use an onsubmit call in your form tag so it will run a validation function before it allows the form to submit. Something like this:
<form name="myform" action="somefile.asp" method="post" onsubmit="return chkFields()">

Give every one of your inclusion fields the same ID of incRadio with a sequential number after each radio group pair like below.
<input type="radio" id="incRadio1" value="yes">
<input type="radio" id="incRadio1" value="no">
<input type="radio" id="incRadio2" value="yes">
<input type="radio" id="incRadio2" value="no">
etc.
Do the same for your exclusion fields.
<input type="radio" id="excRadio1" value="yes">
<input type="radio" id="excRadio1" value="no">
<input type="radio" id="excRadio2" value="yes">
<input type="radio" id="excRadio2" value="no">

Then your javascript function chkFields would work something like this:
Code:
function chkFields() 
{
  //Check Inclusion fields
  for (var x=1; x<=10; x++) 
  {
    if (!document.getElementById('incRadio'+x).checked) 
    {
      alert("All inclusions should be answered Yes!");
      return false;
    }
  }  
  //Check Exclusion fields
  for (var x=1; x<=10; x++) 
  {
    if (!document.getElementById('excRadio'+x).checked) 
    {
      alert("All Exclusions should be answered No!");
      return false;
    }
  }  
  return true;
}

This can be simplified somewhat but I left it this way so you can more easily see what it is doing.
I have not tested the above script but make the needed changes to your form and test it and if it does not work then post your code and any error and we can troubleshoot.


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top