Jump to content
MGLP

Testing a radio button ON or OFF?

Recommended Posts

My HTML is:

[While($cntr < $NumRcds)
{$cntr=$cntr+1;
print("[<input type=\"radio\" name=\"choice\" id=\"rcd\" value=\"$Row[6]\"><br>");}]

which gives me many records listed with each having a radio button.

Upon Submit, I want to test if at least 1 button has been checked.

My Java is the following and I've tried various other versions non of which work.

Can you tell me what the code should be.

[function formcheck()
{if(name.choice.checked==true){return true;}
else{alert(\"At least one record must be selected in order to DELETE.\");return false;}}]

Thank you

 

Share this post


Link to post
Share on other sites

First, your code is generating duplicate ID attributes, all the checkboxes have the same ID "rcd".

You seem to be just making guesses as to how Javascript works instead of trying to understand it, you should take the time to read through the whole W3Schools tutorial for Javascript.

What you need in your Javascript is to get a list of checkboxes. There are several functions in the Document Object Model that do that, getElementsByName works best here.

function formcheck() {
  var checkboxes = document.getElementsByName("choice");
  var atLeastOneIsChecked = false;
  for(var i = 0; i < checkboxes.length; i++) {
    if(checkboxes[i].checked) {
      atLeastOneIsChecked = true;
      break;
    }
  }
  
  if(!atLeastOneIsChecked) {
    alert("At least one record must be selected in order to DELETE.");
    return false;
  }
}

This code will only work, of course, if your form calls this function during the submit event.

Share this post


Link to post
Share on other sites

I've tried this code and it doesn't work and my Function gets executed on subit because I get the alert message from the first test.

Share this post


Link to post
Share on other sites

Then look at the browser console for errors and find why it's not working.

Share this post


Link to post
Share on other sites

You need to clarify what you are trying to do. You are not allowed to have two identical id values on the same page because that is illegal HTML. Also these id values seem to be unnecessary -- they don't serve any purpose. Also if these radio buttons all have the same name then ONLY one of them can be checked.

Share this post


Link to post
Share on other sites

But... is he oversimplifying in his example? Does he ever use more than one name value?

Share this post


Link to post
Share on other sites
On 4/5/2018 at 8:24 AM, MGLP said:

My HTML is:

[While($cntr < $NumRcds)
{$cntr=$cntr+1;
print("[<input type=\"radio\" name=\"choice\" id=\"rcd\" value=\"$Row[6]\"><br>");}]

which gives me many records listed with each having a radio button.

Upon Submit, I want to test if at least 1 button has been checked.

My Java is the following and I've tried various other versions non of which work.

Can you tell me what the code should be.

[function formcheck()
{if(name.choice.checked==true){return true;}
else{alert(\"At least one record must be selected in order to DELETE.\");return false;}}]

Thank you

 

What language are you trying to write with?  Java or Javascript?

There is not a native "print" function in Javascript.  If the code is Java, then you would get better answers in that forum.

 

Share this post


Link to post
Share on other sites
On 5/21/2018 at 3:38 AM, dsonesuk said:

The part of code using 'print' is PHP.

Then why not ask in the PHP forum?

 

Share this post


Link to post
Share on other sites

Because the question is how to validate with Javascript.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×