user3557849
user3557849

Reputation: 55

Validation, how it make in jquery

I have two main fields. Pesel and Nip.

But if one is filled the other does not have to be filled.

For Example, If I complete the field Nip, I don't have to fill field Pesel, but I can do it.

$("#form1").validate({
  submitHandler:function(form) {
  SubmittingForm();

},
   rules: {
      imie: "required", 
      adres_email: {                // compound rule
        required: true,
        email: true
      },
      nazwisko: {                // compound rule
        required: true,
      },
      sid: {
        required: true,
        minlength: 9,
        sid:true
      },
       pesel: {
         minlength: 11,
         pesel:true
       },
       nip: {
         required: true
       },
   }

   });
   });
</script>  

I download it from the link: bassistance.de/jquery-plugins/jquery-plugin-validation

Upvotes: 0

Views: 461

Answers (1)

G.Mendes
G.Mendes

Reputation: 1201

Based on documentation, you will need to use require_from_group method, where you will need to assign pesel and nip to a group in common:

//script narrowed to the issue
$( "#form1" ).validate({
  rules: {
    pesel: {
      require_from_group: [1, ".example-group"]
    },
    nip: {
      require_from_group: [1, ".example-group"]
    }
  }
});

In this example Fiddle I've set up, we have this form:

<form id="myform">
  <label for="pesel">Pesel: </label>
  <input class="left example-group" id="pesel" name="pesel">
  <br/>
  <label for="nip">Nip: </label>
  <input class="left example-group" id="nip" name="nip">
  <br/>
  <input type="submit" value="Validate!" />
 </form>

Notice the class provided to script .example-group which will tell what combo of fields will have the feature (nip + pesel in the case).

EDIT: http://jquery.bassistance.de/validate/additional-methods.js must be included

Upvotes: 2

Related Questions