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
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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.