最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript dropdown validation - Stack Overflow

programmeradmin2浏览0评论

I need to alert if you haven't selected anything and couldn't get it to work. It needs to show what is where wrong and alert with a window.

<!DOCTYPE html>
<html>
<head>
<select id="ddlView">
<option value="0">Select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>  
<input type="button" onclick="myFunction()" value="select" />

<script>
function Validate()
{
var e = document.getElementById("ddlView");
var strUser = e.options[e.selectedIndex].value;

var strUser1 = e.options[e.selectedIndex].text;
if(strUser==0)
{
alert("Please select a user");
}
}
</td></head>

</html>

I need to alert if you haven't selected anything and couldn't get it to work. It needs to show what is where wrong and alert with a window.

<!DOCTYPE html>
<html>
<head>
<select id="ddlView">
<option value="0">Select</option>
<option value="1">test1</option>
<option value="2">test2</option>
<option value="3">test3</option>
</select>  
<input type="button" onclick="myFunction()" value="select" />

<script>
function Validate()
{
var e = document.getElementById("ddlView");
var strUser = e.options[e.selectedIndex].value;

var strUser1 = e.options[e.selectedIndex].text;
if(strUser==0)
{
alert("Please select a user");
}
}
</td></head>

</html>
Share Improve this question asked Mar 12, 2013 at 17:35 user1794625user1794625 1052 gold badges2 silver badges9 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 6

I think you have multiple problems here.

  1. You need to add a body tag
  2. Set the correct function name in your button.
  3. you need a closing script tag

Give this a try:

<!DOCTYPE html>
<html>
    <body>
        <select id="ddlView">
            <option value="0">Select</option>
            <option value="1">test1</option>
            <option value="2">test2</option>
            <option value="3">test3</option>
        </select>  
        <input type="button" onclick="Validate()" value="select" />

        <script type="text/javascript">
            function Validate()
            {
                var e = document.getElementById("ddlView");
                var strUser = e.options[e.selectedIndex].value;

                var strUser1 = e.options[e.selectedIndex].text;
                if(strUser==0)
                {
                    alert("Please select a user");
                }
            }
        </script>
    </body>
</html>

This is not you asked for but you can still consider what i did. Have an option with value 0 which is going to be the default option of the dropdown and add required attribute to the select element. Once this select element is in a form and is submitted ,HTML is automatically going to take over the validation part.

<select required>
<option value="">Select</option>
<option value="1">Option1</option>
</select>

<body>
<div>
    <span>Select the course : </span>
    <select id="ddlView">
        <option value="0">Select</option>
        <option value="1">Java Script</option>
        <option value="2">CSS</option>
        <option value="3">JQuery</option>
        <option value="3">HTML</option>
        <option value="3">C#</option>
    </select>
</div>
<br >
<input type="button" class="btn" value="Submit" id="btnSubmit" onclick="ddlValidate();" />
<script type="text/javascript">
    function ddlValidate() {
        var e = document.getElementById("ddlView");
        var optionSelIndex = e.options[e.selectedIndex].value;
        var optionSelectedText = e.options[e.selectedIndex].text;
        if (optionSelIndex == 0) {
            alert("Please select a Course");
        }
        else {
            alert("Success !! You have selected Course : " + optionSelectedText); ;
        }
    }
</script>

That has to be in your form processing script, you can just add it to the top of your code and check if it has been submitted.

// Place this atop of your script.
if(isset($_POST)) {
    if($_POST['member_id']) {
        // Your codes here
    }
}

call this function at a button click(OnClientClick=" return Validate()") you have to list an item for the 0'th positions(ddlView.Items.Insert(0, new ListItem("Select...", "0"));)

function Validate(){
    var region = document.getElementById('<%=ddlView.ClientID%>').value;
                   if (region == 0) {
                       alert("Please select a user");
                       return false;
                   }
                }

Nowadays, I do not believe it is necessary to alert. You can simply use the required HTML attribute in the select tag and make selected one option by default, disable it, set the value='', and set the button type=submit. In this case, the form won't be submitted if there is no selection by the user.

<form>
  <label for="ddlView">Select an Option</label>
  <select id="ddlView" required>
    <option selected disabled value="">Select</option>
    <option value="1">test1</option>
    <option value="2">test2</option>
    <option value="3">test3</option>
  </select>
  <button type="submit">Submit</button>
</form>

发布评论

评论列表(0)

  1. 暂无评论