javascript not working for radio buttons....

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
sunil.23413
Forum Newbie
Posts: 6
Joined: Fri Jun 20, 2008 1:48 am

javascript not working for radio buttons....

Post by sunil.23413 »

hello,
i m using the folloing javascript for all the forms of my web. This script works well for all the fields...but not for radio buttons due to the reason that i have two radio buttons of the same name.
but if the radio button names are different the script works well.but due to some programming i cant give different names to two radio buttons....

Code: Select all

function MM_findObj(n, d) { 
//alert(n)
//alert(d)
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n);  return x;
}
 
function MM_validateForm() { 
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
//alert(args.length-2)
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    //alert(val)
 
    if (val) { nm=args[i+1]; if ((val=val.value)!="") {
 
               // alert(val)
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } }  else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } 
 
//alert(document.getElementById('agreement1').value)
//alert(document.getElementById('agreement').value)
 
//alert("work")
//if(document.referrer.getElementById('agreement1').value=='1') errors+='Must accept terms of conditions ref';
 
 
//if(document.getElementById('agreement').value=='1') errors+='Must pt terms of conditions';
 
 
 
 
if (errors) alert('The following error(s) occurred:\n'+errors);
 
  document.MM_returnValue = (errors == '');
 
}
 
the form where i m applying this script is given below.if a field is blank, on submit a alert box appears conveying the information about field set black...this works well for the field "state" but not for the field "gender"....your sugestions are most welcomed.........

Code: Select all

<table width="100%" border="0" cellspacing="0" cellpadding="0">
 <form name="register" action="index.php?mode=register" method="post" >
                    <tr>
                        <td colspan="4" class="form-main-bold">Create your account <span class="box-main-bold">Please give your login and brief personal details</td>
                   </tr>
 
                   <tr>
                        <td align="left" valign="middle" class="box-main-bold">Gender : </td>
                        <td colspan="2" valign="top" class="form-text"><input name="gender" type="radio" id="gender" value=""  />
                          Male &nbsp;
                          <input type="radio" name="gender" id="gender" value="" />
                          Female</td>
                  </tr>
                   <tr>
                        <td align="left" valign="top" class="box-main-bold">Current Location : 
             </td>   <input type='text' class="textbox" name="state" id="state" value="" maxlength="20" />< /td>
                  </tr>
 
                   <tr>                      
                        <td width="222" align="right" valign="middle" class="TextTD_Right"><input name="Submitregis" type="submit" class="button" id="Submitregis" value="Submit and Create Account "  onClick="MM_validateForm('gender','Gender :','R','state','State :','R');return document.MM_returnValue" /></td>
                     </tr>
 
</table>
 
adroit
Forum Commoner
Posts: 37
Joined: Fri Aug 08, 2008 1:25 am
Location: India
Contact:

Re: javascript not working for radio buttons....

Post by adroit »

Hi Friend,

Use getElementsByName() instead of getElementById
Post Reply