0

I have validation on form field which works ok. When I click on element in page, I take value of it, put it into form field and I want to change min value in validation to this value I tried rules method but it looks it is not working, but no error is shown

this is my validation call

$('form').validate({
lang: 'sk',
rules: {
  'payment[amount]': {
    min: 1
  }
},
highlight: function(element) {
  $(element).parent().addClass('state-error');
},
unhighlight: function(element) {
  $(element).parent().removeClass('state-error');
}

});

and this is my function running when I click on element

 $( "rewardbox" ).click(function() {
  $val = $( this ).children("amount").text();
  $num = parseInt($val);
  $("#payment_amount").val($num);
  $('html, body').animate({ scrollTop: 0 }, 'slow');

  $( "#form" ).rules( "remove", "min" );
  $("#form").rules("add", {
      'payment[amount]':{
      min: function ()  { return $("#payment_amount").val()  }
    }
  });
});

I tried to put as min value directly my variable $num but no luck

how this should work?

0

2 Answers 2

1

When using the .rules() method, it does not get attached the form element. It only gets attached to the input field where you're attaching the new rule.

Since you would attach .rules() to the input itself, you do not declare the input name inside of it. As per the docs, just key:value pairs of the rules being added or removed and/or a custom messages object.

$("#payment_amount").rules("remove", "min");
$("#payment_amount").rules("add", {
    min: 2 
});

Please refer to usage examples in the documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

have you placed validations after click function ..

i mean

$(document).ready(function(){

// click function (in click function append required value to a hidden field)

// then add validation (in validation min: function ()  { return $("#payment_amount").val()  } this will work )

});

3 Comments

updated with new fiddle jsfiddle.net/medashiva/5RrGa/1635 need to add class to the form input field.
this last works when I click on submit...not what I was looking for exactly but at least something..thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.