1

I am trying to write a validation block inside my JS to check if the user has selected a value or not and pop up a message if the value is not selected.

function validate(form) {
    var success = true;
    var message = "";

    if (form.dropdown.selectedIndex ==  0 )  {  
        form.save.disabled=true;

        if (0 < message.length) {
            message += "\n"; 
        }
        message += "dropdown  value should be selected.";
    }

    if (0 < message.length) {
        alert(message);
        success = false;
    }

    return success;
}

When I click on Save button I call this function. I don't see errors in the logs. Can anyone tell me what am I doing wrongly here? Or can you guide me what is the correct method to validate if the user has selected a value before allowing to save?

Thanks!

3
  • @smiley—don't use tabs for indenting, it messes with SO's formatting. Commented Sep 20, 2012 at 22:53
  • Can you use jQuery? It would make this easier. Commented Sep 20, 2012 at 22:53
  • Your code works for me: jsfiddle.net/jd5Ka. It must be somewhere else in your code. Commented Sep 20, 2012 at 22:54

1 Answer 1

3

If no option is selected, the select element's selectedIndex will be -1. It's recommended that one option is always set to selected, that way you know what the default selected option is (user agents may make the first option selected by default if you don't).

So the test:

if (form.dropdown.selectedIndex ==  0 )  {  

will only be true if the first option has the selected attribute. So either test against -1 or make the first option selected by default, e.g.

<select name="dropdown" ...>
  <option selected value="default">Please select an option
  ...
</select>
Sign up to request clarification or add additional context in comments.

2 Comments

THis is how i am populating the list .... <% con= DataObjectConnectionPool.getInstance().getConnection(); dPreparedStatement = con.prepareStatement("SELECT RTRIM(LTRIM(X.OWNER)) AS OWNER FROM ( SELECT DISTINCT RTRIM(LTRIM(OWNER)) AS OWNER FROM MGD_CONTENT WHERE OWNER IS NOT NULL AND 0 < LENGTH(RTRIM(LTRIM(OWNER)))) X ORDER BY LOWER(RTRIM(LTRIM(X.OWNER))) "); dResultSet = dPreparedStatement.executeQuery(); while (dResultSet != null && dResultSet.next()) { String owner = dResultSet.getString("OWNER"); if (ownerArray == null) { ownerArray = new ArrayList(); } ownerArray.add(owner); %>
<tr> <td valign="middle" align="right" width="135"><label for="dropdown">User id:<span class="font_red"><strong>*</strong></span></label></td> <td><img src="common/images/shim.gif" border="0" height="1" width="10" alt=""/> <select name="dropdown" id="dropdown"> <option value="">Select Your User ID</option> <% for (int b = 0; ownerArray != null && b < ownerArray.size(); b++) { String ownerx = (String) ownerArray.get(b); %> <option value="<%=ownerx%>"><%=ownerx%></option> <% } %> </select> </td> <td valign="middle" align="left"></td> </tr>

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.