SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Radio button validation

    Hi all,

    I am new to this forum, but have many questions regarding javascript.

    In this case, I have a form which contains two radio button groups: (yes/no

    The idea is that the user is forced to select for each group one option, yes or no, and if they choose yes they have to fill in some text boxes.

    I also have a general form validation, validation from Dreamweaver.

    Everything is working ok, all validations are ok, except when the user does leaves the form blank and chooses NO in both radio groups. In this case, the validations stop working and the form is submitted with all fields blank.

    This is my code for the validations. Thanks in advance for any help.


    PHP Code:
    <script type="text/javascript">
    <!--
    function 
    validate(nForm){

            var 
    firstQ nForm['firstSet'];
            var 
    secondQ nForm['secondSet'];
            if (!
    firstQ[0].checked && !firstQ[1].checked)
                {
                 
    alert('Por favor, indicanos si estas trabajando en la industria');
    //             return false;
                
    }
            if (!
    secondQ[0].checked && !secondQ[1].checked)
                {
                 
    alert('Por favor, indicanos si estas estudiando cine');
                 return 
    false;
                }
            if (
    firstQ[0].checked)
                {
                 if (
    nForm['Area'].value == "")
                    {
                     
    alert('Por favor, indicanos cual es tu area de trabajo');
    //                 return false;
                    
    }
                }
    //        else    {
    //             document.getElementById('firstNew1').disabled = true;
    //             document.getElementById('firstNew2').disabled = true;
    //            }
            
    if (secondQ[0].checked)
            {
                 if (
    nForm['Escuela'].value == "" || nForm['especialidad'].value == "")
                    {
                     
    alert('Por favor, indicanos cual es tu especialidad y en que escuela estas estudiando');
                     return 
    false;
                    }
                }
    //        else     {
    //             document.getElementById('secondNew1').disabled = true;
    //             document.getElementById('secondNew2').disabled = true;
    //            }

            
    return true
        }
            

    function 
    MM_findObj(nd) { //v4.01
      
    var p,i,x;  if(!dd=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        
    d=parent.frames[n.substring(p+1)].documentn=n.substring(0,p);}
      if(!(
    x=d[n])&&d.allx=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(!
    && d.getElementByIdx=d.getElementById(n); return x;
    }

    function 
    MM_validateForm() { //v4.0
      
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
      for (
    i=0i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
        if (
    val) { nm=val.name; if ((val=val.value)!="") {
          if (
    test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
            if (
    p<|| p==(val.length-1)) errors+='- '+nm+' debe ser una direccion de e-mail valida.\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<numerrors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
        } } } else if (
    test.charAt(0) == 'R'errors += '- '+nm+' es obligatorio.\n'; }
      } if (
    errorsalert('Ocurrieron los siguientes errores:\n'+errors);
      
    document.MM_returnValue = (errors == '');
    }
    //-->
    </script> 
    and this is the onsubmit property of the form

    PHP Code:
    <form action="carga_con_control.asp" method="post" name="form1" onsubmit="MM_validateForm('Nombre','','R','Apellido','','R','email','','RisEmail');return validate(this);return document.MM_returnValue"

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    London, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you aware that it says "retur n" and not "return" in this code:

    Code:
    onsubmit="MM_validateForm('Nombre','','R','Apellido','','R','email','','RisEmail');retur  n validate(this);return document.MM_returnValue"
    If the validation doesn't work, perhaps you get an error. Use Firefox and open the Javascript console and see what you get there.

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    peterbe,

    It seems that the space appeared when I pasted the code. It is ok in the original code.

    Still having the same problem. Any ideas?

    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •