Validate if person and legal age

Asked

Viewed 1,355 times

3

Good afternoon staff, how to check if a person is of age with the plugin jQuery.validationEngine?

I need the form only validates if the person is over 18 years old, if they are under 18 years old they are blocked.

1 answer

2


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 in 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>
<script src="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>
<form action="" method="post" id="form1" name="form1">
  <input type="text" id="idade" name="idade" class="validate[required,funcCall[checkIdade]]">
  <button type="submit">Enviar</button>
</form>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.