0

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?

4
  • 2
    That isn't a combobox; it's a dropdown. Commented Jun 16, 2014 at 21:15
  • 1
    You're correct. I updated it. Thanks Commented Jun 16, 2014 at 21:18
  • Try document.forms["registration"]["range_date"] != null && document.forms["registration"]["range_date"] != undefined Commented Jun 16, 2014 at 21:22
  • Your problem does not originate from the sample you provided, eventhough it's not 100% correct. It runs: jsfiddle.net/Vznaw Commented Jun 16, 2014 at 21:28

1 Answer 1

1

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) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.