Solved

Include a radio group in validation

Posted on 2006-06-12
1
207 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now