MGLP Posted April 5, 2018 Share Posted April 5, 2018 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 Link to comment Share on other sites More sharing options...
Ingolme Posted April 5, 2018 Share Posted April 5, 2018 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. Link to comment Share on other sites More sharing options...
MGLP Posted April 5, 2018 Author Share Posted April 5, 2018 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. Link to comment Share on other sites More sharing options...
Ingolme Posted April 6, 2018 Share Posted April 6, 2018 Then look at the browser console for errors and find why it's not working. Link to comment Share on other sites More sharing options...
davej Posted April 6, 2018 Share Posted April 6, 2018 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. Link to comment Share on other sites More sharing options...
dsonesuk Posted April 6, 2018 Share Posted April 6, 2018 But on initial load they are all unchecked, hence the validation to test if any checked Link to comment Share on other sites More sharing options...
davej Posted April 6, 2018 Share Posted April 6, 2018 But... is he oversimplifying in his example? Does he ever use more than one name value? Link to comment Share on other sites More sharing options...
JMRKER Posted May 21, 2018 Share Posted May 21, 2018 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. Link to comment Share on other sites More sharing options...
dsonesuk Posted May 21, 2018 Share Posted May 21, 2018 The part of code using 'print' is PHP. Link to comment Share on other sites More sharing options...
JMRKER Posted May 22, 2018 Share Posted May 22, 2018 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? Link to comment Share on other sites More sharing options...
justsomeguy Posted May 22, 2018 Share Posted May 22, 2018 Because the question is how to validate with Javascript. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now