0

I am available with the solution given by @Tomalak for MY QUESTION could you pls help me out with it as its giving me an error in firebug as : frm.creatorusers is undefined [Break On This Error] var rdo = (frm.creatorusers.length >...rm.creatorusers : frm.creatorusers; I used the code for validating radio button as:

function valDistribution(frm) {
var mycreator = -1;
        var rdo = (frm.creatorusers.length > 0) ? frm.creatorusers : frm.creatorusers; 
         for (var i=0; i<rdo.length; i++) {
         if (rdo[i].checked) {
         mycreator = 1;
            //return true;
          }
          }
          if(mycreator == -1){
          alert("You must select a Creator User!");
  return false;

          }

}
2
  • WHoever is downvoter give me the reason for ur vote Commented Jan 11, 2011 at 6:58
  • I did not vote you down however: 1) you are NOT using the correct code given to you 2) you do not show where you are calling valDistribution and 3) why not FINISH one question before asking the same again? Commented Jan 11, 2011 at 8:42

2 Answers 2

2

Here is how to use the code you were given by @Tomalak but did not copy correctly

function valDistribution(frm) { // frm needs to be passed here
  var myCreator=false;
// create an array if not already an array
  var rdo = (frm.creatorusers.length > 0) ? frm.creatorusers : [frm.creatorusers];  
  for (var i=0; i<rdo.length; i++) {
    if (rdo[i].checked) {
      myCreator=true;
      break; // no need to stay here
    }
    if (!myCreator){
      alert("You must select a Creator User!");
      return false;
    }
    return true; // allow submission
  }

assuming the onsubmit looking EXACTLY like this:
<form onsubmit="return valDistribution(this)">

and the radio NAMED like this:
<input type="radio" name="creatorusers" ...>

Sign up to request clarification or add additional context in comments.

Comments

2

You can try this script:

<html>
  <script language="javascript">
   function valbutton(thisform) {
   myOption = -1;
   alert(thisform.creatorusers.length); 
   if(thisform.creatorusers.length ==undefined) {
     alert("not an array");
     //thisform.creatorusers.checked = true;
     if(thisform.creatorusers.checked) {
       alert("now checked");
       myOption=1;              
       alert("You selected button number " + myOption
         + " which has a value of "
         + thisform.creatorusers.value);    
     }
   }
   else {
     for (i=thisform.creatorusers.length-1; i > -1; i--) {
       if (thisform.creatorusers[i].checked) {
         myOption = i; i = -1;
       }
     }
     if (myOption == -1) {
       alert("You must select a radio button");
       return false;
     }
     alert("You selected button number " + myOption
       + " which has a value of "
       + thisform.creatorusers[myOption].value);
   }
}
</script>
    <body>
    <form name="myform">
<input type="radio" value="1st value" name="creatorusers" />1st<br />
<!--<input type="radio" value="2nd value" name="creatorusers" />2nd<br />-->
<input type="button" name="submitit" onclick="valbutton(myform);return false;" value="Validate" />
<input type="reset" name="reset" value="Clear" />
</form> 
</body>    
</html>

8 Comments

MAnn its displaying Error thats y i have reposted the question with same link, else why would I have reposted it?
As available in the previous{linked} question it can have mulitple radio buttons and also it ca have a single radio button, Its dynamic
I have modified the code to accept single and multiple radio buttons
The forum may not be able to give you the exact working code, but can point out work around.
@UNNI Its still gives me same error "frm.creatorusers is undefined"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.