Show alert if no radio button selected

My code snippet shows two different forms and sets of JavaScript. I  need to combine both so if no radio button is selected, an alert is shown.

The second option is the one I need to use so I'll need to be shown a full version of my code, JavaScript and form changes to get this to work correctly.
<!--- This method works for the radio button. If no radio button is selected, an alert is shown.  --->
<script>
function theRadioCheck(){
  theForm = document.forms.myForm;
  for(var i=0;i<theForm.LogInType.length;i++){
    if(theForm.LogInType[i].checked) return true;
  }
  alert("No selection made. New or returning user?");
  return false;
}

</script>

<form name="myForm" method="post" onSubmit="return theRadioCheck()" >


New Player: <input type="radio" name="LogInType" value="0" ><br>
Returning player: <input type="radio" name="LogInType" value="1" > 
<br>
<input type="submit" value="Submit" >

</form>
<!--- End of radio button test --_>

<!--- The code below works except for the radio values. I need to combine both JavaScripts so that if a radio button is not selected, a Javascript alert is shown. --->

<SCRIPT language="JavaScript">
function validateForm(form) {

   if (form.LOGIN_TYPE.value == "") {
      alert("No selection checked. Are you a Returning user, or a New user?");
      return false
   }  


   if (form.HANDLE.value == "") {
      alert("Enter a user name.");
      return false
   }
   
      
   if (form.PASSWORD.value == "") {
      alert("Enter a password.");
      return false
   }

   return true
}

</SCRIPT>
</head>
<body>
<form  method="post" action="" onSubmit="return validateForm(this)">
 Current Member: <input type="radio" name="LOGIN_TYPE" value="M"><br />
 New User: <input type="radio" name="LOGIN_TYPE" value="N"><br />
 Your name:  <input type="text" name="HANDLE"><br />
 Password: <input type="password" name="PASSWORD" value="">
 <input type="submit" value="Login" >
</form>
</body>
</html>

Open in new window

QsorbAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
a fast way :


onSubmit="return validateForm(this) && theRadioCheck()"

Open in new window

0
QsorbAuthor Commented:
Had no idea this could be doen. Anyway, I tried it but it didn't work for me. No validation for any inputs now.

It probably has something to do with form names, references, etc. There are so many possibilities, and that's why, in my original question, I very clearly stated:

"I'll need to be shown a full version of my code, JavaScript and form changes to get this to work correctly."

I'll share points if anyone can complete this as asked and give me a working set of Javascripts and one complete form.
0
leakim971PluritechnicianCommented:
it should be great to clean the HTML and ask help for updating the javascript
check the following :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script>

	function theRadioCheck(form){
		for(var i=0;i<form.LogInType.length;i++){
			if(form.LogInType[i].checked) return true;
		}
		alert("No selection made. New or returning user?");
		return false;
	}

	function validateForm(form) {
		if (!form.LOGIN_TYPE[0].checked && !form.LOGIN_TYPE[1].checked) {
			alert("No selection checked. Are you a Returning user, or a New user?");
			return false
		}  
		if (form.HANDLE.value == "") {
			alert("Enter a user name.");
			return false
		}
		if (form.PASSWORD.value == "") {
			alert("Enter a password.");
			return false
		}
		return true
	}

</script>
</head>
<body>
<form  method="post" action="" onSubmit="return validateForm(this) && theRadioCheck(this)">
Current Member: <input type="radio" name="LOGIN_TYPE" value="M"><br />
New User: <input type="radio" name="LOGIN_TYPE" value="N"><br />
Your name:  <input type="text" name="HANDLE"><br />
Password: <input type="password" name="PASSWORD" value="">
<input type="submit" value="Login" >
New Player: <input type="radio" name="LogInType" value="0" ><br>
Returning player: <input type="radio" name="LogInType" value="1" > 
<br>
</form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QsorbAuthor Commented:
Alright. Seems simple and obvious now. But that's what Bush said to Clinton!

Many thanks for clearing this up and saving me tons of work, and teaching me how to combine two functions.
0
leakim971PluritechnicianCommented:
You're welcome! Thanks for the points!

As you can see we used two way to check the radio button, the loop is useful if you've a lot of radio buttons

>Seems simple and obvious now. But that's what Bush said to Clinton!
xlol

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.