views:

180

answers:

2

I have my Select box in following manner, which i cannot change.

<select name='box2' id='box2'>
    <option value="select"> Select </option>
    <option value='hundread'> hundread </option>
    <option value='two'> two </option>
</select>

Now i've to make the validation as required, by keeping class="required" will not help me, coz first element has value as "select". Now is there any way to make validation to not select the 1st element based on selectedIndex.

If you may help me by giving custom class rule which i can put directly on tag like

<select name='box2' id='box2' class="required[---some rule to check selectedIndex is not 0---]">
A: 

Why don't you compare the selected value to "select"

$("#box2").val() != "select"
rahul
Thx Rahul for quick response, but i cannot do the same.As my form is getting shared by more than 26 pages. Form is like a container here, and several pages are embossing their contents to this single form. Form has onsubmit check function. I want a custom class to pass a false value. I am already checking the form like$(document).ready(function() { $("#myform").validate({ }); });
swatantra
+1  A: 

I got the solution, I made below in view

<select name='box2' id='box2' class="MustSelectOpt">

<script type="text/javascript">
    $(document).ready(function() {
        $.validator.addMethod("MustSelectOpt", function(value, element) {
        if(element.selectedIndex <= 0) return element.selectedIndex;
                else return value;
    }, " You must select any Option.");

    $("#axis_placement_form").validate();
  });
</script>

and attached one library to add custom method jquery.validate.js

swatantra