View Single Post
Old 01-23-2013, 10:59 PM   PM User | #1
superwookie
New Coder

 
Join Date: Jul 2012
Posts: 61
Thanks: 14
Thanked 5 Times in 5 Posts
superwookie is an unknown quantity at this point
Javascript Form Verification Issue

Hi guys! I try to learn languages by picking up examples and working my way through them logically, but I'm stuck on a piece of Javascript for form verification. I was wondering if you might be able to help me out.

I'm trying to verify a form with two different types of data-- a text field and a group of radio buttons. Here's the HTML for the form, cleand up to remove some table formatting and other fluff that I don't believe is causing issues:

Code:
<form name="myForm" action="mailto:xxx.xxx@xxx.com" method="POST" enctype="text/plain" onsubmit="return validateForm()">

<p>opName:</p>
<p><input type="text" name="opName" length="40"></p>

<p>Product:</p>
<p><input type="radio" name="product" checked="yes" value="0" style="display: none;">
<input type="radio" name="product" value="bert"> Bert &nbsp; 
<input type="radio" name="product" value="ernie"> Ernie &nbsp; 
<input type="radio" name="product" value="elmo"> Elmo</p> 

<input type="submit" value="Submit">

</form>
All well and good. The form works fine for my purposes. What I'm having issues with is verification. I'm using a hidden, selected radio button with a value of "0" as a default selection for the "product" variable. Here's the code I'm trying to use to verify the form before it's submitted:

Code:
<script>

function validateForm()
{
var x=document.forms["myForm"]["opName"].value;
var y=document.forms["myForm"]["product"].value;

if (x==null || x=="")
  {
  alert("You must enter your opName.");
  return false;
  }

if (y=="0")
  {
  alert("You must select a product.");
  return false;
  }
}

</script>
While the opName will validate without any issues, the product will not. Can someone point me in the right direction of what I'm doing wrong here? I've tried everything I can think of, and haven't had any luck. Any help is greatly appreciated.

Thanks to y'all, you're always great!

Last edited by superwookie; 01-23-2013 at 11:09 PM..
superwookie is offline   Reply With Quote