Jump to content

trouble validating if radio button selected


simpli

Recommended Posts

Hi,

I am trying to validate in my form validation if a radio button was selected. I have radio buttons that give options between 2 choices and basically I want to stop the validation process if no choice has been made. I get to the part where i call the validation function but I dont seem to be executing what is in the function for some reason. Again, i am new to js I am going through the Tizag tutorial but trying the monkey see monkey do approach at the same time.

 

Thanks for any help. My code is below,

J-R

 

<script type='text/javascript'>

 

function formValidator(){

// Make quick references to our fields

var matchup1 = document.getElementById('1vs8East');

 

alert("calling validation");

alert(matchup1.value);

if(radioselected(matchup1, "You must choose a team")){

return true;

}

 

return false;

 

}

 

function radioselected(elem, helperMsg) {

 

chosen = ""

len = elem.length

alert("in validation");

alert(len);

 

for (i = 0; i <len; i++) {

if (elem.checked) {

chosen = elem.value

}

}

 

if (chosen == "") {

alert("no choice made")

elem.focus();

return false;

}

return true

}

 

</script>

 

<?php

/*Form header information */

print <<<htmldebut

<form id="frmchoixronde1" name="frmchoixronde1"  method="POST"

enctype="application/x-www-form-urlencoded" onsubmit='return formValidator()'>

 

<b>Entrez vos choix pour la premiere ronde</b>

htmldebut;

    include('textmenu.css');

echo '<html><head><title>Page des poolers</title></head> <body>';

 

//If we get here as a result of the submit button having been pushed: We are going to validate the data

if(isset($_POST['submit'])){

echo 'Server side validation';

 

}

 

?>

 

<?php

 

 

echo '<input type="submit" name="submit" value="Soumettre vos choix">';

//Shows Eastern conference matchups

echo '<br/>';

echo '<fieldset>';

echo '<legend> Est</legend>';

 

echo '<input name="1vs8East" type="radio" id="1vs8East_1" value="Boston">Boston</label><label for="1vs8East_2"><input name="1vs8East" type="radio" id="1vs8East_2" value="Buffalo">Buffalo</label>';

echo '</fieldset>';

 

//End of the form

?>

</form>

<br/>

</body></html>

Link to comment
Share on other sites

First of all, the HTML is all out of wack! You're writing JS to the page before you have even opened the HTML tag. I've stripped out all the PHP in the code below - just use that format for generating the page

 

<html>
<head>
<title>Page des poolers</title>

<script type="text/javascript">

function formValidator(formObj)
{
   if(!radioSelected(formObj['1vs8East']))
   {
      alert("You must choose a team");
      return false;
   }

    alert("Validation passes");
    return true;
}

function radioSelected(radioGrp)
{
   if (radioGrp.length)
   {
      //There are 2 or more options
      for (var i=0; i<radioGrp.length; i++)
      {
         if (radioGrp[i].checked) { return true; }
      }
      //No options were checked
      return false;
   }
   else
   {
      //There is only 1 option
      return radioGrp.checked;
   }
}

</script>

</head>

<body>

<form id="frmchoixronde1" name="frmchoixronde1"  method="POST" enctype="application/x-www-form-urlencoded" onsubmit='return formValidator(this)'>

<b>Entrez vos choix pour la premiere ronde</b>

<input type="submit" name="submit" value="Soumettre vos choix">
<br/>
<fieldset>
  <legend> Est</legend>
  <label for="1vs8East_1"><input name="1vs8East" type="radio" id="1vs8East_1" value="Boston">Boston</label>
  <label for="1vs8East_2"><input name="1vs8East" type="radio" id="1vs8East_2" value="Buffalo">Buffalo</label>
</fieldset>

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