Validate if the person is of legal age

1

Good afternoon, how do I check if a person is of legal age with the jQuery.validationEngine plugin?

I need the form to only validate if the person is over 18, if it is under 18, it is blocked.

    
asked by anonymous 08.09.2016 / 17:11

1 answer

1

Create a function that will determine your validation:

function checkIdade(field, rules, i, options){
  if (field.val() < 18) {
     // this allows the use of i18 for the error msgs
     return options.allrules.validate2fields.alertText;
  }
}

Then call your input on class="validate[required,funcCall[checkIdade]]"

<input class="validate[required,funcCall[checkIdade]]" 
       type="text" 
       id="idade" 
       name="idade" />

funcCall [methodName]

function checkIdade(field, rules, i, options) {
  if (field.val() < 18) {
    return options.allrules.validate2fields.alertText;
  }
}
$(document).ready(function() {
  $("#form1").validationEngine();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/jquery.validationEngine.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/validationEngine.jquery.min.css" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-Validation-Engine/2.6.4/languages/jquery.validationEngine-en.js"></script><br><br><formaction="" method="post" id="form1" name="form1">
  <input type="text" id="idade" name="idade" class="validate[required,funcCall[checkIdade]]">
  <button type="submit">Enviar</button>
</form>
    
08.09.2016 / 17:25