开发者

Validating a single radio button is not working in available javascript validation script Part-2

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;

    开发者_如何学Python      }

}


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜