Related Posts Plugin for WordPress, Blogger...

About

Follow Us

Wednesday, 22 April 2015

Introduction: 
In this article I will explain how to validate (check) HTML Select DropDownList using JavaScript.  

The HTML Button has been assigned a JavaScript OnClick event handler. When the Button is clicked, the Validate JavaScript function is executed,  if the selected value matches the value of the default item then an error message is displayed using JavaScript alert message box.
Validate (Check) HTML Select DropDownList using JavaScript
<script type="text/javascript">
    function Validate() {
        var ddlFruits = document.getElementById("ddlEmp");
        if (ddlFruits.value == "") {
            //If the "Please Select" option is selected display error.
            alert("Please select an option!");
            return false;
        }
        return true;
    }
</script>
Full Source Code of sample Application:
Design your page as given Below(With JavaScript script in head section)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Validate HTML dropdown using Javascript</title>

</head>
<body>
<form id="form1">
 <fieldset style="width:350px">
    <legend><strong>Validate HTML dropdown using Javascript</strong></legend>
    <br />
Select Employee:
<select id="ddlEmp">
    <option value=""></option>
    <option value="1">Ashish</option>
    <option value="2">Anuj</option>
    <option value="3">Anil</option>
</select>
<input type="submit" value="Validate" onclick="return Validate()" />
<br /><br />
<script type="text/javascript">
    function Validate() {
        var ddlFruits = document.getElementById("ddlEmp");
        if (ddlFruits.value == "") {
            //If the "Please Select" option is selected display error.
            alert("Please select an option!");
            return false;
        }
        return true;
    }
</script>
</fieldset>
</form>
</body>

</html>
Categories: ,

0 comments:

Post a Comment