Jquery define multiple custom validation to same input field using jquery validator
- <form action="" method="post">
- <p>
- <input data-validation="even_number" type="text">
- </p>
- ...
- </form>
- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
- <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.2.1/jquery.form-validator.min.js"></script>
- <script>
-
- // Add validation
- $.formUtils.addValidator({
- name : 'even_number',
- validatorFunction : function(value, $el, config, language, $form) {
- return parseInt(value, 10) % 2 === 0;
- },
- errorMessage : 'You have to give an even number',
- errorMessageKey: 'badEvenNumber'
- });
-
- // Setup form validation
- $.validate();
-
- </script>