Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Radio Button Validation

Posted on 2009-02-08
5
Medium Priority
?
156 Views
Last Modified: 2012-05-06
Hi - Why would a javascript function recognize the first radio button but not the second if they are both named the same thing?  The function will validate the first radio button as being checked but if the second radio button is checked, it tells me that I still need to make a selection.
This is running on a server and not on the web.  Thanks!
function checkRadio1(main)  {
var checked = false;
var buttons = document.main.PLAN;
var pnvalu = document.main.INVOICE.value;
var size = pnvalu.length;
var comp = "ABC"
var size3 = pnvalu.substring(0,3);
 
pnvalu = pnvalu.toUpperCase();
size3 = size3.toUpperCase();
 
 
for (var i=0; i<buttons.length; i++) {
            if (buttons[i].checked)   
                        {
                        checked = true;
                        if(size < 9)
                                    {
                                    alert("PLEASE ENTER A CORRECT INVOICE NUMBER - It's missing or too short")
                                    }
                        else if(size > 9)
                                    {
                                    alert("PLEASE ENTER A CORRECT INVOICE NUMBER - It has too many characters")
                                    }
                        else if(size3 != comp)
                                    {
                                    alert("PLEASE ENTER A CORRECT INVOICE NUMBER - Must begin with HXI")
                                    }
                        else
                                    {
                                    savetofile1();
                                    }
                        }
 
            if(!checked)
                        {
                        alert("You need to choose a plan!");
                        return checked ;
                        }
                                                }
                                    
                                    }

Open in new window

0
Comment
Question by:MoJeaux39
  • 3
5 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 23585623
We need to see the HTML in order to see what is going wrong.
0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23585728
Try it this way
function checkRadio1(main)
{
  var checked = false;
  var buttons = document.main.PLAN;
  var pnvalu = document.main.INVOICE.value;
  var size = pnvalu.length;
  var comp = "ABC"
  var size3 = pnvalu.substring(0,3);
 
  pnvalu = pnvalu.toUpperCase();
  size3 = size3.toUpperCase();
 
  for (var i=0; i<buttons.length; i++)
  {
    if (buttons[i].checked)   
    {
      checked = true;
      if(size < 9)
      {
        alert("PLEASE ENTER A CORRECT INVOICE NUMBER - It's missing or too short")
      }
      else if(size > 9)
      {
        alert("PLEASE ENTER A CORRECT INVOICE NUMBER - It has too many characters")
      }
      else if(size3 != comp)
      {
         alert("PLEASE ENTER A CORRECT INVOICE NUMBER - Must begin with HXI")
      }
      else
      {
          savetofile1();
      }
    }
  }
  if(!checked)
  {
    alert("You need to choose a plan!");
    return checked ;
  }
}
}

Open in new window

0
 
LVL 16

Expert Comment

by:sunithnair
ID: 23585736
One bracket extra sorry
function checkRadio1(main)
{
  var checked = false;
  var buttons = document.main.PLAN;
  var pnvalu = document.main.INVOICE.value;
  var size = pnvalu.length;
  var comp = "ABC"
  var size3 = pnvalu.substring(0,3);
 
  pnvalu = pnvalu.toUpperCase();
  size3 = size3.toUpperCase();
 
  for (var i=0; i<buttons.length; i++)
  {
    if (buttons[i].checked)   
    {
      checked = true;
      if(size < 9)
      {
        alert("PLEASE ENTER A CORRECT INVOICE NUMBER - It's missing or too short")
      }
      else if(size > 9)
      {
        alert("PLEASE ENTER A CORRECT INVOICE NUMBER - It has too many characters")
      }
      else if(size3 != comp)
      {
         alert("PLEASE ENTER A CORRECT INVOICE NUMBER - Must begin with HXI")
      }
      else
      {
          savetofile1();
      }
    }
  }
  if(!checked)
  {
    alert("You need to choose a plan!");
    return checked ;
  }
}

Open in new window

0
 
LVL 16

Accepted Solution

by:
sunithnair earned 2000 total points
ID: 23585746
the problem is bacause you had the if condition to show the error message was in the for loop and if the 2nd checkbox is checked it would show the error message and exit out of the for loop because of the return statement
0
 

Author Closing Comment

by:MoJeaux39
ID: 31544316
This works perfect now!  Thank you for the explanation and assist!  
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

581 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