Jump to content

checkform not working


harkly

Recommended Posts

Can someone help me with my checkform??

 

I am trying to check if one of 2 radio buttons has been checked, if neither one is checked I want an alert stating so.

 

THis is the code I am trying to modify.

 

<script type="text/javascript">
  function checkRadio(field)
  {
    for(var i=0; i < field.length; i++)
      {
        if(field[i].checked) return field[i].value;
      }
         return false;
  }


  function checkForm(form) 
    {
      if(radioValue = checkRadio(form.gender))
        {
          alert("You selected " + radioValue);
          return true;
        }
      else
        {
          alert("Error: No value was selected!");
          return false;
        }
    }
</script>

 

 

I thought that if I did this it would work but of course it doesn't, been playing around with it for way to long :(

 

<script type="text/javascript">
  function checkRadio(field)
  {
    for(var i=0; i < field.length; i++)
      {
        if(field[i].checked) return field[i].value;
      }
         return false;
  }


  function checkForm(form) 
    {
      if(radioValue != checkRadio(form.gender))
        {
          alert("Error: No value was selected!");
          return false;
        }

          return true;
        }
    }
</script>

 

Radio's on my form::

<br>Gender: <input type='radio' id='male' name='gender' value='2' class='outline'> Male  
              <input type='radio' id='female' name='gender' value='1' class='outline'> Female 

Link to comment
https://forums.phpfreaks.com/topic/204883-checkform-not-working/
Share on other sites

You are misunderstanding this line:

if(radioValue = checkRadio(form.gender))

 

This is setting the variable 'radioValue' equal to whatever checkRadio() returns and then the if statement checks if it is true or false. Since you changed it to this:

if(radioValue != checkRadio(form.gender))

 

You are checking if the return value of checkRadio() is NOT EQUAL to a variable 'radioValue' that, as far as I can tell, isn't even defined anywhere so it will always be false and ultimately return true.

 

If you simply change it to this it should work:

if(!checkRadio(form.gender))

 

If that is confusing, take a look at this simple form as a concept:

<html>
<head>
<script type="text/javascript">
function checkForm(frm)
{
   for (var i=0;i<frm.gender.length;i++)
   {
      if (frm.gender.item(i).checked)
         return true
   }
   alert("Gender is not selected");
   return false;
}
</script>
</head>
<body>
<form onsubmit="return checkForm(this)">
<input type="radio" name="gender" value="0" /> Male
<input type="radio" name="gender" value="1" /> Female
<input type="submit" />
</form>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.