Link to home
Start Free TrialLog in
Avatar of henrycrinkle34
henrycrinkle34

asked on

Java Script to validate a radio button on HTML form.

I want to make sure that the radio button has been checked before allowing the form to submit. The form executes ok to the perl script. However it is submitting without the question being answered.

-----------------------------------------------------------------------------------------------------------------
<HTML>
<HEAD>

    <script language="JavaScript">
         function checkForm() {
            if (userResponse.fname.value=='') {
                alert("You must provide a first name");
                event.returnValue=false;
            } else if (userResponse.sname.value=='') {
                      alert("You must provide a surname");
                           event.returnValue=false;
                                }else if (userResponse.trained.value==0)  {
                       alert ("You have not answered Q1");
                       event.returnValue=false;
                                }
      }      
    </script>

</HEAD>

<BODY>

<!-- Use of form -->
<FORM name="userResponse" ACTION="http://kenbane.infc.ulst.ac.uk/~32181502/cgi-bin/thesis/response.pl">

First name:<INPUT TYPE="text" NAME="fname" size="25" VALUE="">

Q1. Have you received any bioinformatics training.
<input type=radio name=trained value=yes>Yes
<input type=radio name=trained value=no>No
<BR><BR>

<!-- Submission & validation -->
<CENTER>
<INPUT type="submit" VALUE="Submit" onclick="checkForm()">
<INPUT TYPE="reset" VALUE="Reset">
</CENTER>
Avatar of NetGroove
NetGroove

Here my proposal:

<HTML>
<HEAD>
<script language="JavaScript">
function checkForm(theForm) {
  msg = "";
  if (theForm.fname.value=='') {
    msg += "\nYou must provide a first name";
  }
  if (theForm.sname.value=='') {
    msg += "\nYou must provide a sure name";
  }
  qTrain = theForm.trained;
  qCheck = false;
  for(i=0;i<qTrain.length;i++){
    if(qTrain[i].checked==true){
      qCheck = true;
    }
  }
  if (qCheck == false) {
    msg += "\nYou have not answered Q1";
  }
  if (msg>"") {
    alert ("Errors found:"+msg);
    event.returnValue=false;
  }
}    
</script>
</HEAD>

<BODY>

<!-- Use of form -->
<FORM name="userResponse" ACTION="http://kenbane.infc.ulst.ac.uk/~32181502/cgi-bin/thesis/response.pl" onSubmit="return checkForm(this)">

First name:<INPUT TYPE="text" NAME="fname" size="25" VALUE="">

Last name:<INPUT TYPE="text" NAME="sname" size="25" VALUE="">

Q1. Have you received any bioinformatics training.
<input type=radio name=trained value=yes>Yes
<input type=radio name=trained value=no>No
<BR><BR>

<!-- Submission & validation -->
<CENTER>
<INPUT type="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
</CENTER>
</form>
</body>
</html>


ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
NetGroove

>>>event.returnValue=false;<<<

NS will throw an error on this and it will not return false, the form will submit anyway.
Thanks jAy.

Here the revised version:
<HTML>
<HEAD>
<script language="JavaScript">
function checkForm(theForm) {
  msg = "";
  if (theForm.fname.value=='') {
    msg += "\nYou must provide a first name";
  }
  if (theForm.sname.value=='') {
    msg += "\nYou must provide a sure name";
  }
  qTrain = theForm.trained;
  qCheck = false;
  for(i=0;i<qTrain.length;i++){
    if(qTrain[i].checked==true){
      qCheck = true;
    }
  }
  if (qCheck == false) {
    msg += "\nYou have not answered Q1";
  }
  if (msg>"") {
    alert ("Errors found:"+msg);
    return false
  }
}    
</script>
</HEAD>

<BODY>

<!-- Use of form -->
<FORM name="userResponse" ACTION="http://kenbane.infc.ulst.ac.uk/~32181502/cgi-bin/thesis/response.pl" onSubmit="return checkForm(this)">

First name:<INPUT TYPE="text" NAME="fname" size="25" VALUE="">

Last name:<INPUT TYPE="text" NAME="sname" size="25" VALUE="">

Q1. Have you received any bioinformatics training.
<input type=radio name=trained value=yes>Yes
<input type=radio name=trained value=no>No
<BR><BR>

<!-- Submission & validation -->
<CENTER>
<INPUT type="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
</CENTER>
</form>
</body>
</html>

Since i already wrote it up i will post anyway.

<HTML>
<HEAD>
<script language="JavaScript">
function checkRadioControl(strFieldName){
      bolSelected = false;
      for (i=0;i<strFieldName.length;i++){
      if(strFieldName[i].checked){
            bolSelected = true;
      break;
      }
}    
      if(!bolSelected){
            return false;
            }
      return true;
}

function checkForm(fObj){
var strErr = "";
  if (fObj["fname"].value == ""){
      strErr += "- First Name\n";
  }
  if(fObj["sname"].value == ""){
     strErr += "- Surname\n";
  }
  if(!checkRadioControl(fObj["trained"])){
     strErr += "- You have not answered Q1\n";
  }
  if(strErr != ""){
       alert("The Following Fields Contain Errors:\n\n"+ strErr);
             return false;
     }
   return true;
}
</script>

</HEAD>

<BODY>

<!-- Use of form -->
<FORM name="userResponse" ACTION="http://kenbane.infc.ulst.ac.uk/~32181502/cgi-bin/thesis/response.pl" onSubmit="return checkForm(this)">

First name:<INPUT TYPE="text" NAME="fname" size="25" VALUE="">
Surname:<INPUT TYPE="text" NAME="sname" size="25" VALUE="">
Q1. Have you received any bioinformatics training.
<input type=radio name=trained value=yes>Yes
<input type=radio name=trained value=no>No
<BR><BR>

<!-- Submission & validation -->
<CENTER>
<INPUT type="submit" VALUE="Submit">
<INPUT TYPE="reset" VALUE="Reset">
</CENTER>
</form>
</body>
</html>
Take care! We are observed by the Queen :-)
Hey, did you see that I am the featured Expert of our topic area!
When finally will that gif feature be available?
>>>When finally will that gif feature be available?
what to show your pic?

You have been featured forever.
wait until you see my face :)
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: fritz_the_blank {http:#9730907}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jAy
EE Cleanup Volunteer