cancel
Showing results for 
Search instead for 
Did you mean: 

Adding a condition to a custom javascript validator

Adding a condition to a custom javascript validator

Hi guys, I recently added a custom validator to a dropdown field and would like to add a condition to it so that the validation will not trigger if the value of a certain dropdown list is not Yes.  How can I modify my code to add the condition statement?

 

Here's my code:

<script type="text/javascript">
require([
    'jquery', // jquery Library
    'jquery/ui', // Jquery UI Library
    'jquery/validate', // Jquery Validation Library
    'mage/translate' // Magento text translate (Validation message translte as per language)
], function($){ 
$.validator.addMethod(
    'validate-item-duration', function(value) {
        return (value > 0);
    }, $.mage.__('This is a required field.'));
});
</script>
1 REPLY 1

Re: Adding a condition to a custom javascript validator

ok nevermind, i found the solution.  i did not realize until today code i provided is a jquery validator.  jeez i am getting stressed out on this Magento stuff my mind is working no more.

 

<script type="text/javascript">
require([
    'jquery', // jquery Library
    'jquery/ui', // Jquery UI Library
    'jquery/validate', // Jquery Validation Library
    'mage/translate' // Magento text translate (Validation message translte as per language)
], function($){ 
$.validator.addMethod(
    'validate-item-duration', function(value) {
        var x = $(".clsRahahaIsPosted").val();
        if (x == "No") return true; 
        return (value > 0);
    }, $.mage.__('This is a required field.'));
});
</script>