Parsley - Examples | Custom validator

### Parsley

Tweet

## Craft your own validators demo

<< Back to examples

Do you code? *

Yes No

If yes, in which language(s)? Do you eat dog food? *

Yes No

If no, why?

    <form id="demo-form" data-parsley-validate>
      <label for="question">Do you code? *</label>
      <p>
        <input type="radio" name="question" value="true" required /> Yes
        <input type="radio" name="question" value="false" /> No
      </p>

      <label for="languages">If yes, in which language(s)?</label>
      <input type="text" class="form-control" name="languages" data-parsley-conditionalrequired='["[name=\"question1\"]:checked", "yes"]' data-parsley-validate-if-empty data-parsley-success-class="" data-parsley-conditionalrequired-message="This value is required since you are a programmer!" />

      <label for="question">Do you eat dog food? *</label>
      <p>
        <input type="radio" name="question2" value="yes" required /> Yes
        <input type="radio" name="question2" value="no" /> No
      </p>

      <label for="why">If no, why?</label>
      <input type="text" class="form-control" name="why" data-parsley-conditionalrequired='["[name=\"question2\"]:checked", "no"]' data-parsley-validate-if-empty data-parsley-success-class="" data-parsley-conditionalrequired-message="This value is required since you do not eat dog food!" />

      <input type="submit" class="btn btn-default pull-right" />
    </form>

    <script type="text/javascript">
      window.ParsleyConfig = {
        validators: {
          conditionalrequired: {
            fn: function (value, requirements) {
              // if requirements[0] value does not meet requirements[1] expectation, field is required
              if (requirements[1] == $(requirements[0]).val() && '' == value)
              return false;

              return true;
            },
            priority: 32
          }
        }
      };
    </script>
    <script type="text/javascript" src="../../dist/parsley.js"></script>