0

I want to validate my selectboxes and I tried different approaches.

I don't want allow user to use default value = "Please select".

Validation is working only on first time, but when I'm submitting the form validation is passing.

My HTML select:

      <select id="website_category" name="website[category]" class="selectBox" style="display: none; "><option value="">--- please select ---</option>
       <option value="Banking">Banking</option>
       <option value="Computers">Computers</option>

< Coupons <

Here is my code:

 jQuery.validator.addMethod("notEqual", function(value, element, param) {
   return value != param;
  }, "Please select something");
  ...
  'website[category]': {
              notEqual: "--- please select ---"
        },

also tried

 required:true

Here is what I get. When I choose default value gives me:

but when I submit validation is passing:

WHY it is happening like that ? Can someone suggest right solution ?

4

1 回答 1

-1

最好的方法是将其设置为禁用,如下所示:

<select id="website_category">
 <option value="-1" disabled selected="selected">---please select---</option>
  ....
</select>

然后您检查客户端和服务器端的值 -1 以确保安全。

一旦用户更改了选择,您就不能再次选择该选项。这也强制执行错误检查。

于 2012-10-05T10:09:43.783 回答