Reputation: 319
I have the following sample of html code:
<form id="registration" action="process.php" onsubmit="return validateForm()" method="post">
<label for="range_date" class="form">Select Range Date</a></label>
<select name="range_date">
<option value="" style="display:none;"></option>
<option value="June 27th 2014 6:00pm">June 27th 2014 6:00pm</option>
<option value="June 27th 2014 8:00pm">June 27th 2014 8:00pm</option>
</select>
</form >
And the following sample js code:
if (document.forms["registration"]["range_date"] != 'undefined')
{
var range_date=document.forms["registration"]["range_date"].value;
if (range_date==null || range_date=="")
{
alert("Please select a Range Date");
return false;
}
}
I get an error in firebug stating: "document.forms.registration.range_date is undefined" but I don't expect it to reach the inside of the if statement. Any thoughts?
Upvotes: 0
Views: 433
Reputation: 207521
That is because 'undefined' != undefined
.
Just use a truthy check
if (document.forms["registration"]["range_date"]) {
or drop the quotes
if (document.forms["registration"]["range_date"]===undefined) {
Upvotes: 1