By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,968 Members | 1,602 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,968 IT Pros & Developers. It's quick & easy.

Strange problem with field verification in IE

P: n/a
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
Aug 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Mike the Canadian wrote:
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">
The language attribute is depreciated, type is required.

<script type="text/javascript">
<!--
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

--
Rob
Aug 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.