Print Page | Close Window

validate radio button

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: General Discussion
Forum Description: General discussion and chat on any topic.
URL: https://forums.webwiz.net/forum_posts.asp?TID=15722
Printed Date: 31 March 2026 at 1:54pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: validate radio button
Posted By: Lucent
Subject: validate radio button
Date Posted: 04 July 2005 at 7:10pm

Hi, I have these 2 validate function, how can put them together?

My main purpose is add validation for 3 radio button group to the existing validation form I found from webwiz site.

 

I tried added the red part, but still doesn't work.
  

this part is taken from web wiz CONDTS form

------------------------------

function Checkform() {

 var errorMsg = "";

 if (document.Form.firstName.value == ""){
  errorMsg += "\n\tFirst Name \t- Enter your First Name";
 }
 
 if (document.Form.lastName.value == ""){
  errorMsg += "\n\tLast Name \t- Enter your Last Name";
 }
 
 if (document.Form.contact.value == ""){
  errorMsg += "\n\tContact \t- Enter your contact number";
 } 
 
 if (document.Form. radioButton1.checked == true){
  errorMsg += "\n\tType \t- Enter the type";
 } 
 
 if ((document.Form.email.value == " mailto:%22%20%20document.formContact.email.value.length%20%3e%200%20&&%20document.formContact.email.value.indexOf%22@%22,0 - ") || (document.formContact.email.value.length > 0 && (document.formContact.email.value.indexOf("@",0 ) == - 1 || document.formContact.email.value.indexOf(".",0) == - 1))) {
  errorMsg += "\n\tE-mail Address \t- Enter your valid e-mail address";
 }
  
 if (errorMsg != ""){
  msg = "______________________________________________________________\n\n";
  msg += "Enquiry has not been sent because there are problems with the form.\n";
  msg += "______________________________________________________________\n\n";
  msg += "The following fields need to be corrected: -\n";
  
  errorMsg += alert(msg + errorMsg + "\n\n");
  return false;
 }
 return true;
}

------------------------------

 
 
I found this other fuction that actually validate the radio group one by one. If the above statement doesn't work, is there any way I can incroporate the following code to it?

-------------------------------

function validateForm (form) {
  for (var e = 0; e < form.elements.length; e++) {
    var el = form.elements[e];
      if (el.type == 'radio') {
      var group = form[el.name];
      var checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check ' + el.name + ' radio button');
        el.focus();
        return false;
      }
    }
  }
  return true;
}

------------------------------------------

 




Replies:
Posted By: dj air
Date Posted: 04 July 2005 at 7:17pm
what errors are you getting as i have thois section below working using radios

EmailSSLPrice = "0.00" * "0"

if (document.Calc.CustomError.checked == true){
CustomErrorPrice = "0.50" * "1"
}
else
{
CustomErrorPrice = "0.00" * "0"
}

with no problems

remember javascript is case sensativeWink


Posted By: Lucent
Date Posted: 05 July 2005 at 12:39am
I didn't get any error, it simply just ignore it, it validates others but the radio button.


Posted By: Lucent
Date Posted: 05 July 2005 at 12:55am
here is example of my code
 
------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Contact Us</title>
<SCRIPT  language="JavaScript">
function CheckForm () {
 var errorMsg = "";
 if (document.frmEnquiry.firstName.value == ""){
  errorMsg += "\n\tFirst Name \t- Enter your First Name"; 
 }
 if (document.frmEnquiry.lastName.value == ""){
  errorMsg += "\n\tLast Name \t- Enter your Last Name";
 }
    if (document.frmEnquiry. radiobutton.checked == true){
        errorMsg += "\n\tType \t- Enter the type";
    }
 if (document.frmEnquiry.enquiry.value == "") {
   errorMsg += "\n\tEnquiry \t\t- Enter an enquiry";
 }
 if (errorMsg != ""){
  msg = "______________________________________________________________\n\n";
  msg += "Your enquiry has not been sent because there are problem(s) with the form.\n";
  msg += "Please correct the problem(s) and re-submit the form.\n";
  msg += "______________________________________________________________\n\n";
  msg += "The following field(s) need to be corrected: -\n";
  
  errorMsg += alert(msg + errorMsg + "\n\n");
  return false;
 }
 
 return true;
}
// -->
</script>
<!-- Close the HTML document head -->
</head>
<!-- Start the HTML body -->
<body text="#000000" bgcolor="#FFFFFF" link="#0000FF" vlink="#990099" alink="#FF0000">
<table width="85%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td>
      <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td>
            <form method="post" name="frmEnquiry" action="send_email.asp" onSubmit="return CheckForm();">
              <table width="450" border="0" align="center" cellpadding="0" cellspacing="0" >
                <!-- First tabe row -->
                <tr>
                &nbs p; <td colspan="3"> <font size="2"><b>*Indicates required fields
                &nbs p;   </b></font>
                </tr>
                <!-- second table row -->
                <tr>
                &nbs p; <td width="265">First Name* <br>
                &nbs p;   <input maxlength="30" name="firstName">
                &nbs p; </td>
                &nbs p; <!-- Coloum two in the second table row -->
                &nbs p; <td width="185" colspan="2">Last Name* <br>
                &nbs p;   <input maxlength="30" name="lastName">
                &nbs p; </td>
                </tr>
                <!-- Third table row spread over 3 coloumns -->
                <tr>
                &nbs p; <td width="265">E-mail Address* <br>
                &nbs p;   <input maxlength="255" name="email">
                &nbs p; </td>
                &nbs p; <td width="185" colspan="2">&nbsp;</td>
                </tr>
                <tr>
                &nbs p; <td colspan="3">&nbsp;</td>
                </tr>
                <tr>
                &nbs p; <td colspan="3"><input name="radiobutton" type="radio" value="radiobutton1">
                &nbs p;   1
                &nbs p;     <input name="radiobutton" type="radio" value="radiobutton2">
                &nbs p;     2</td>
                </tr>
                <tr>
                &nbs p; <td colspan="3">&nbsp;</td>
                </tr>
                <td colspan="3" height="170">Enquiry* <br>
                &nbs p; <textarea name="enquiry" cols="50" rows="10"&g


Posted By: dj air
Date Posted: 05 July 2005 at 7:28am
not sure on all of it, but

((document.frmEnquiry.email.value == " mailto:%22" document.frmenquiry.email.value.length=" - 0   is missing ") and the end of that one, and any js after that will fail


Posted By: Lucent
Date Posted: 05 July 2005 at 11:48am
how about this one? it still validate other fields but the radio button.
------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Contact Us</title>
<SCRIPT  language="JavaScript">
 
//Check the enquiry form is filled in correctly
function CheckForm () {

 //Initialise variables
 var errorMsg = "";

 //Check for a name
 if (document.frmEnquiry.name.value == ""){
  errorMsg += "\n\tName \t\t- Enter your Name"; 
 }
   
 //Check for comments
 if (document.frmEnquiry.comments.value == "") {
   errorMsg += "\n\tComments \t- Enter your comments";
 }
 
 //Check for gender
 if (document.frmEnquiry.gender.checked == true) {
   errorMsg += "\n\tGender \t- Select your gender";
 }

  
 //If there is aproblem with the form then display an error
 if (errorMsg != ""){
  msg = "______________________________________________________________\n\n";
  msg += "Your enquiry has not been sent because there are problem(s) with the form.\n";
  msg += "Please correct the problem(s) and re-submit the form.\n";
  msg += "______________________________________________________________\n\n";
  msg += "The following field(s) need to be corrected: -\n";
  
  errorMsg += alert(msg + errorMsg + "\n\n");
  return false;
 }
 
 return true;
}
// -->
</script>
</head>
<body>
<table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td>
      <table width="200" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td>
            <form method="post" name="frmEnquiry" action="send_email.asp" onSubmit="return CheckForm();">
              <table width="200" border="0" align="center" cellpadding="0" cellspacing="0" >
                <tr> 
                 <td>Name* <br>
                &nbs p; <input maxlength="30" name="name">
                &nbs p; <br>
                &nbs p; <input name="gender" type="radio" value="male">
male
<input name="gender" type="radio" value="female">
female * <br>
Comments* <br>
<textarea name="comments" cols="20" rows="2"></textarea>
<br>
<input type="submit" name="Submit" value="Submit"></td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</body>
</html>

---------------------------
 
if the above statement doesn't work is there any way I can incroporate the following statement into the current one?
----------------------------
function validateForm (form) {
  for (var e = 0; e < form.elements.length; e++) {
    var el = form.elements[e];
      if (el.type == 'radio') {
      var group = form[el.name];
      var checked = false;
      if (!group.length)
        checked = el.checked;
      else
        for (var r = 0; r < group.length; r++)
          if ((checked = group[r].checked))
            break;
      if (!checked) {
        alert('Please check ' + el.name + ' radio button');
        el.focus();
        return false;
      }
    }
  }
  return true;
}
----------------------------


Posted By: dj air
Date Posted: 05 July 2005 at 3:59pm
you are using values male and female

not true and false try that.




Posted By: Lucent
Date Posted: 06 July 2005 at 1:03am
I'm not sure what that mean.
 
like
<input name="gender" type="radio" value="true">
or
<input name="gender" type="radio" value="0">
 
I tried them both, neither works...Cry


Posted By: pmormr
Date Posted: 13 July 2005 at 2:38am
the checked property will return FALSE if it isn't checked... your telling it to give you the message if it IS checked. try

//Check for gender
if (document.frmEnquiry.gender.checked == false) {
   errorMsg += "\n\tGender \t- Select your gender";
}

i don't program in JavaScript... that's just my best guess having had experience in C++ and Visual Basic

-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net