3

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>

6 Answers 6

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

Comments

2

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>

enter image description here

Comments

1
<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>

1 Comment

More explanation of how your code differs from OP and why that makes it work may be useful.
0

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
    }
}

1 Comment

there is no mention of PHP in the question (or tags or in the provided code)
0

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;
                   }
                }

Comments

0

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>

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.