Solved

Include a radio group in validation

Posted on 2006-06-12
1
210 Views
Last Modified: 2013-11-18
I have this simple JS and works about 90% ok.

I need to include a part taht will validate a radio group to make sure an option has been selected.



<script language="JavaScript" type="text/JavaScript">


function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " ") { temp = ""; }
   return temp;
}

function validateEmail(fieldValue) {
     return (/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/).test(fieldValue);
}


function Validate() {
              document.forma.nombre.value=trim(document.forma.nombre.value)
              if (document.forma.nombre.value.length == 0) {
               alert("Falta Nombre.")
                return(false)               }      

               document.forma.telefono.value=trim(document.forma.telefono.value)
               if (document.forma.telefono.value.length == 0) {
                alert("Falta Telefono.")
                return(false)                  }

        
              if (!validateEmail(document.forma.email.value)) {
           alert("La dirección de correo electrónico no es valida.");
           return false;
               }      




        return(true)
       


}



</script>
0
Comment
Question by:robrodp
1 Comment
 
LVL 10

Accepted Solution

by:
0h4crying0utloud earned 500 total points
ID: 16889117
somn like this should work:

function Validate() {
              document.forma.nombre.value=trim(document.forma.nombre.value)
              if (document.forma.nombre.value.length == 0) {
               alert("Falta Nombre.")
                return(false)              }    

               document.forma.telefono.value=trim(document.forma.telefono.value)
               if (document.forma.telefono.value.length == 0) {
                alert("Falta Telefono.")
                return(false)               }

       
            if (!validateEmail(document.forma.email.value)) {
          alert("La dirección de correo electrónico no es valida.");
          return false;
              }    


         var myOption = -1;
         for (i=document.forma.radioGroupName.length-1; i > -1; i--) {
           if (document.forma.radioGroupName[i].checked) {
             myOption = i;
           }
         }
         if (myOption == -1) {
            alert("You must select a radio button");
            return (false);
         }

        return(true)
       


}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question