Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking results in a form

Status
Not open for further replies.

Castanz

Programmer
Apr 5, 2002
61
US
I am posting data from a form. I want to check if the data is valid and make a choice on the result. After I make the choice and go through the error checking, the variable "yesno" is null.

This is the code that checks the value yesno:
Code:
if (frm.yesno.value == "Yes"){ 
   alert (frm.yesno.value) //This returns a null value so the above condition is never met.
if (frm.Date_Treatment.value  == "")   
alert("Please enter a Date ")
frm.Date_Treatment.focus()
return false
}

This is the code in the form:
Code:
<select Name=&quot;yesno&quot; size=&quot;1&quot;>
      <option Value=&quot;Yes&quot; Selected>Yes </option>
      <option Value=&quot;No&quot;>No </option>
    </select>

If I choose a field that a user inputs in at text box, there is no problem with checking the field.

I hope this is clear enough. Can anyone help?

Thanks in advance and thanks for reading my post.

Al castanz@yahoo.com
 
How is the function supposed to know what
Code:
frm
is if you haven't passed it any reference?

Maybe this example will show you what I mean ...
Code:
<script>
function testing( frm ) {

  if ( frm.yesno.value == &quot;Yes&quot; ) { 
    alert ( frm.yesno.value );
  }
}
</script>

<form>
<select Name=&quot;yesno&quot; size=&quot;1&quot; onchange=&quot;testing( this.form );&quot;>
      <option Value=&quot;Yes&quot; Selected>Yes </option>
      <option Value=&quot;No&quot;>No </option>
    </select> 
</form>

HTH.
 
Your trying to get the value from a Select box, you need to access the options property.

frm.yesno.value

to

alert(frm.yesno.options.value);

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
Sry forgot one part

alert(frm.yesno.options(frm.yesno.selectedIndex).value);

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
No he doesn't — it'll work as
Code:
yesno.value
.

Should he really be going via selectedIndex I wonder? Anyone got any idea on what's considered &quot;best practice&quot;.
 
If my previous post seems a bit out-of-synch that's because it was supposed to be in reply to your first post, not your second, Mr Greed. Bloody forum. ;)
 
I make it a practice to return the value from a select box using the selectedIndex approach.

I like the use, because it tells the coder right aways your dealing with a select box and not an input field.

Just the way I code.

MrGreed

&quot;did you just say Minkey?, yes that's what I said.&quot;
 
You're right, it does let you know that you're dealing with a select, but I'm not so sure that's always desirable.

Well, no matter.
 
Theboyhope - I did pass the values. My code works for text box values, but not for pull down menus.

MrGreed - When I use your first post with options.value. I get frm.yesno.options has no properties. I am not sure how to implement your second post. What do I use for an index.

Is there anyway I can make a pull down menu act like a text box? If I write the form code this way it works, but I know there must be a way to make it work with a pull down menu.
Code:
<input type=&quot;text&quot; name=&quot;yesno&quot; size=&quot;11&quot;>
 
Replace your
Code:
frm.yesno.value
with
Code:
frm.yesno.options[frm.yesno.options.selectedIndex].value
and see if that works.
 
I get frm.yesno.options has no properties when I debug.
 
Hmmm ... can we get a fuller example of your code, please. What browser are you testing this in btw?
 
Take the options out of the last post and we got it
alert (&quot;test value: &quot; +frm.yesno.options[frm.yesno.selectedIndex].value)}

Thanks again for all the help.

:)
 
Glad to hear it. :)

Indulge my curiosity though, please: what browser are you using?
 
For testing I use Netscape 4.7. I am sorry that is a good point. I like the javascript: command for debugging. Normally I use IE. Your code may have worked with IE.

Thanks Again,
Al
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top