Isuru
Isuru

Reputation: 3184

JavaScript working in IE8 and 9 but not in Chrome

I have this javascript to see if the drop-down is selected. If not an error message is displayed. The problem is that the code works in IE9 and 8, but not in Chrome.

The simple solution to this is to add the "required" attribute to the tag, but I want to know the reason that code isn't working.

<form name="form1" action="insert.php" method="post" onsubmit="return validateForm()">
<table name="table1">
     <tr>
        <td>
            <select name="fruits">
                <option selected disabled>Please Select</option>
                <option value="banana>banana</option>
                <option value="apple>apple</option>
            </select>
        </td>
    </tr>
</table>
<input type="submit" value="Submit"/>
</form>

<script type="text/javascript">
    function validateForm()
    {
    var x=document.forms["form1"]["fruits"].value;
    if (x==null || x=="")
      {
      alert("Please Select Fruit");
      return false;
      }
    }
</script>

Upvotes: 0

Views: 68

Answers (1)

Karthick Kumar
Karthick Kumar

Reputation: 2361

please review this code,required is the html5 attribute and by default check for empty values

<form name="form1" action="insert.php" method="post" onSubmit="return validateForm()">
    <table name="table1">
        <tr>
            <td>
                <select name="fruits">
                    <option value=" "  disabled>Please Select</option>
                    <option value="banana">banana</option>
                    <option value="apple">apple</option>
                </select>
            </td>
        </tr>
    </table>
    <input type="submit" value="Submit" />
</form>
<script>
    function validateForm() {
        var x = document.forms["form1"]["fruits"].value;

        if (x == null || x == " ") {
            alert("Please Select Fruit");
            return false;
        }
    }
</script>

Demo here: http://jsfiddle.net/2zgGM/

Upvotes: 1

Related Questions