-1

hello i have case in my jquery, ex :

i have drop-down list and text field form :

<select id="id_level"  name="id_level">
<option value=""  > -choose-</option>
<option value="1"  > -Administrator-</option>
<option value="2"  > -Kondektur Bus-</option>
<option value="3"  > -Staf Operasi-</option>
</select>

<input name="job" type="text" id="job"/>

i want validate drop-down list with: required : "must choose" and equal with :

var text_id_level = $("#id_level option:selected" ).text();
    var val_job= $("job").val();

    if(text_id_level !=""){

            if(text_id_level =="Administrator" && (val_job!="Kondektur Bus" || val_job!="Staf Operasi"))
            {
                // equal = true
            }

            else if(text_id_level == val_job) 
            {
                // equal = true
            }

            else
            {
                // equal = false , message : not equal
            }

    }

    else
    {
        // equal = false, message : must choose
    }

how to format code above in jquery validate plugin ? format like

jQuery.validator.addMethod("notEqual", function(value, element, param) {
  return this.optional(element) || value != param;
}, "Please specify a different (non-default) value");

from here >> How to add a Not Equal To rule in jQuery.validation

and this my jquery validate metode

$("#Form").validate({
        rules: {
            id_level:"required",
                        job:"required",

        },
        messages: {
            id_level:"choose one",
                        job:"must fill"
        }
    });

and i don't know how equals metode for my case, sorry for my english

Community
  • 1
  • 1
bukanamay
  • 577
  • 5
  • 11
  • 26
  • Start by reading the [documentation](http://jqueryvalidation.org) and [tag wiki](http://stackoverflow.com/tags/jquery-validate/info). Then make an honest attempt at solving your own problem; don't just expect us to do it for you. – Sparky May 30 '14 at 03:46
  • i have read and i have get stack ... so i ask here ... thanks – bukanamay May 30 '14 at 03:59
  • If that's true, then show us where and how you're stuck. What exactly have you done so far? Where is your call to `.validate()`? What options did you try? What error messages did you get? In other words, you need to ask a very specific question... not just _"I got stuck so do all the work for me."_ – Sparky May 30 '14 at 04:02
  • i'm sorry, this is mistake, can't you look again ? – bukanamay May 30 '14 at 04:10
  • Again, where is the call to the `.validate()` method? The plugin is not initialized on the form without it. – Sparky May 30 '14 at 04:11
  • ok... look again.. please ;) – bukanamay May 30 '14 at 04:19
  • You seemed really desperate for a solution so I did my best for you... the least you could do is respond to my answer below. – Sparky May 31 '14 at 15:41

1 Answers1

0

This will give you a rough idea about how you are supposed to declare multiple rules. I cannot make any further sense out of your question, so you'll need to adjust this accordingly.

$("#Form").validate({
    rules: {
        id_level: "required",     // <- shortcut for one rule
        job: {                    // <- more than one rule on this field
            required: true,  
            notEqual: '#id_level' // <- set your custom rule parameter to id="id_level"
        }
    },
    messages: {
        id_level: "choose one",
        job: {
            required: "must fill" // <- need to specify which rule here
        }
    }
});

You'll also need to tweak your custom method a bit, so that you can obtain the value of the field you're passing in using the param.

jQuery.validator.addMethod("notEqual", function (value, element, param) {
    return this.optional(element) || value != $(param).val();
}, "Please specify a different value");

DEMO: http://jsfiddle.net/Ztjz4/

Sparky
  • 98,165
  • 25
  • 199
  • 285