javascript - JS Validation if dropdown value selected or not - Stack Overflow

I am trying to write a validation block inside my JS to check if the user has selected a value or not a

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!

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!

Share Improve this question edited Sep 20, 2012 at 22:52 RobG 148k32 gold badges179 silver badges214 bronze badges asked Sep 20, 2012 at 22:42 smileysmiley 4914 gold badges15 silver badges37 bronze badges 3
  • @smiley—don't use tabs for indenting, it messes with SO's formatting. – RobG Commented Sep 20, 2012 at 22:53
  • Can you use jQuery? It would make this easier. – Tool Commented Sep 20, 2012 at 22:53
  • Your code works for me: jsfiddle/jd5Ka. It must be somewhere else in your code. – Ethan Brown Commented Sep 20, 2012 at 22:54
Add a ment  | 

1 Answer 1

Reset to default 3

If no option is selected, the select element's selectedIndex will be -1. It's remended 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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742284594a4415099.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信