5

I tried the script of http://jqueryvalidation.org/max-method/.. with examples of static values. how to use the max value but dynamic? I tried with this script but it did not work

jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
      max: "#field1" //from field1 dynamic value???
    //max: 23 //static value
    }
  }
});
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
<form id="myform">
    <lable>value1 : </lable>
    <input type="text" class="left" id="field1" name="field1"><br>
    <lable>no greater than value1 : </lable>
    <input type="text" class="left" id="field" name="field">
      <br/>
<input type="submit" value="Validate!">
</form>

how to work?

0

2 Answers 2

9

Return the value of the field using a function.

$( "#myform" ).validate({
    rules: {
        field: {
            required: true,
            max: function() {
                return parseInt($('#field1').val());
            }
        }
    }
});

Remember to enclose the value within parseInt or the number could be evaluated as a string.

Working DEMO: http://jsfiddle.net/dq7wf3qj/

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

Comments

0

set max as max: function(){ return $("#field1").val();}

jQuery.validator.setDefaults({
  debug: true,
  success: "valid"
});
$( "#myform" ).validate({
  rules: {
    field: {
      required: true,
    max: function(){ return $("#field1").val();}
    }
  }
});
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/additional-methods.min.js"></script>
<form id="myform">
    <lable>value1 : </lable>
    <input type="text" class="left" id="field1" name="field1"><br>
    <lable>no greater than value1 : </lable>
    <input type="text" class="left" id="field" name="field">
      <br/>
<input type="submit" value="Validate!">
</form>

Comments

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.