Strange problem with field verification in IE

M

Mike the Canadian

I am having a strange problem with field verification in a form. The
JavaScript below works just fine in Firefox but in IE. "license" is a
pull-down list and "requiredDiscount" is a text field. When license is
"Freeware" and requiredDiscount is "N/A", in IE (but not Firefox) you
get the message "For trialware you must enter a valid discount". It
seems that in IE "this", the form, is not set. I also tried using
"document.form" instead of "this" but then you can have
requiredDiscount as "N/A" when license is not "Freeware". Can anyone
explain what is wrong here?

-------------------

<html><head>
<script language="javascript">
<!--
function submitIt(form){
var licChoice = form.license.selectedIndex
var sDiscount = form.requiredDiscount.value
if ((form.license.options[licChoice].value == "Freeware") &&
(sDiscount != "N/A")){
alert("For freeware you must enter a discount of N/A")
return false
}
sDiscount = sDiscount.toUpperCase()
if ((sDiscount == "N/A") && (form.license.options[licChoice].value
!= "Freeware")){
alert("For trialware you must enter a valid discount")
return false
}
return true
}
-->
</script>

</head>

<body>
<form onSubmit="return submitIt(this);" action="list.cgi"
name="submitForm" method="post"
enctype="application/x-www-form-urlencoded">
[...]
Non-profit Discount: (e.g.: 10% - enter N/A for freeware)<br>
<textarea name="requiredDiscount" rows="3"
cols="61">N/A</textarea><br>
Software license:<br>
<select name="license"><option
selected="selected">Time-limited trial</option><option>Feature-limited
demo</option><option>Commercial</option><option>Freeware</option></select>
[...]

_______
Trade your DVDs, movies and CDs for Free!
http://www.dvdtrades.net
 
R

RobG

Mike said:
I am having a strange problem with field verification in a form. The
JavaScript below works just fine in Firefox but in IE. "license" is a
pull-down list and "requiredDiscount" is a text field. When license is
"Freeware" and requiredDiscount is "N/A", in IE (but not Firefox) you
get the message "For trialware you must enter a valid discount". It
seems that in IE "this", the form, is not set. I also tried using
"document.form" instead of "this" but then you can have
requiredDiscount as "N/A" when license is not "Freeware". Can anyone
explain what is wrong here?

The language attribute is depreciated, type is required.


HTML comments inside script elements are unnecessary and potentially
harmful if your page is later converted to XHTML without removing the
comment markers. Just don't use them.
function submitIt(form){
var licChoice = form.license.selectedIndex
var sDiscount = form.requiredDiscount.value
if ((form.license.options[licChoice].value == "Freeware") &&

Here is your problem. The value of the option should be the content
(text) if there is no value attribute, however IE doesn't follow the
spec. So use the option's text:

if ((form.license.options[licChoice].text == "Freeware") &&

That will work in all compliant browsers.
(sDiscount != "N/A")){
alert("For freeware you must enter a discount of N/A")
return false
}
sDiscount = sDiscount.toUpperCase()
if ((sDiscount == "N/A") && (form.license.options[licChoice].value

Same here.

Using semi-colons to terminate lines is not strictly necessary but it is
good practice to always put them in.
!= "Freeware")){
alert("For trialware you must enter a valid discount")
return false
}
return true
}
-->
</script>

</head>

<body>
<form onSubmit="return submitIt(this);" action="list.cgi"
name="submitForm" method="post"
enctype="application/x-www-form-urlencoded">
[...]
Non-profit Discount: (e.g.: 10% - enter N/A for freeware)<br>
<textarea name="requiredDiscount" rows="3"
cols="61">N/A</textarea><br>
Software license:<br>
<select name="license"><option
selected="selected">Time-limited trial</option><option>Feature-limited

Using - selected="selected" may trip up some browsers. If your doctype
is HTML, then just the 'selected' attribute should be added with no value.
demo</option><option>Commercial</option><option>Freeware</option></select>

Properly blocking your code makes life much easier for those who would
try to help.
[...]

_______
Trade your DVDs, movies and CDs for Free!
http://www.dvdtrades.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top