Jquery Validate max as per condition

Asked

Viewed 839 times

3

I’m making a jquery.validate, can make max dynamically, something like:

...
    max: function () {
    return $('#QtdEstoqueHidden').val();
...

ok, it worked! (I store the max value in a Hidden field that is dynamically replaced by an ajax on the page)

Now I want to make that depending on a <select> that max be ignored.

I tried to make him ignore the validation, but I could not, so I tried to make a max = 999999999 would be enough as if ingorasse. but I could not. Now he always releases the form.

Follow the jsfiddler https://jsfiddle.net/dorathoto/9nmzntqs/2/

1 answer

4


You can change the rules dynamically!

I added the excerpt below in the fiddle to demonstrate.

When there is a change in the combobox and if the value is 0 it removes the rule, otherwise it returns the rule.

jQuery.validator.setDefaults({
            debug: false,
            success: "valid"
        });
        $("#FormMov").validate({
            rules: {
                Quantidade: {
                    required: true,
                   max: function() {
                      var maxValue = $('#QtdEstoqueHidden').val();
                        return parseInt(maxValue);

                  }
                }
            },
            messages: {
                Quantidade: {
                    required: "é obrigatório",
                    max: "O valor deve conter um número menor que {0}"
                }
            }
        });
         $('select[name=TipoMovimentacao]').change(function(){
	         if ($('select[name=TipoMovimentacao]').val() == 0) {
           			console.log("remove regra");
                $('#Quantidade').rules('remove');
             }else{
            $('#Quantidade').rules('add', {
                max: function () {
                console.log("adiciona regra");
                        var maxValue = $('#QtdEstoqueHidden').val();
                        return parseInt(maxValue);
                    }
            });
             }
         });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>


<form action="#" id="FormMov" method="post" novalidate="novalidate">
  Max: <input type="text" id="QtdEstoqueHidden" value="43">
  <hr>
  <input data-val="true" data-val-number="The field Quantidade must be a number." data-val-required="The Quantidade field is required." id="Quantidade" name="Quantidade" type="number" value="0" aria-required="true" aria-invalid="false">
  <select class="form-control valid" data-val="true" data-val-required="The Tipo Movimentacao field is required." id="TipoMovimentacao" name="TipoMovimentacao" aria-invalid="false"><option value="">Selecione o tipo</option>
<option value="0">Ilimitado</option>
<option selected="selected" value="1">Limitar Max</option>
<option value="2">Limitar Max 2</option>
</select>
  <button type="submit" class="btn btn-primary pull-right">Salvar</button>
</form>

  • Buddy, your fiddle doesn’t work yet either.. If I select the Limit Max option, it should limit the value I type, in your example although the max is 43 accepted 32412312, and if I select unlimited.. then he should ignore.

  • it validates and drop an exception and does not allow to continue with the form Ubmit... or you that it does not allow to enter any number greater than the stipulated?

  • If you can change the attribute max of input type='number' according to the conditions that suit you.

  • vc did not understand.. I am typing 10000 in the input field and it is accepting.. max is not validating when it should validate

  • Strange, but I think I solved, he does not directly accept the return of $().val(), put a parseInt, I updated the code, see if it fits.

  • see, https://s3.postimg.org/l8n6o8roj/Limitar.png although it’s like Limit Max it’s letting me type a higher number than max.

  • It displays the message when it loses input focus: https://postimg.org/image/jaf1bpy47/

  • on my Chrome does not show that! I can even click send button.

  • Are you testing the snippet in the stackoverflow response, where is the exception button? The code is just an example to demonstrate functionality, you must adapt it to your need.

  • no, no fidller q vc posted, ops...was wrong.. I will test.

Show 5 more comments

Browser other questions tagged

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