Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
80
rated 0 times [  81] [ 1]  / answers: 1 / hits: 16814  / 9 Years ago, thu, october 1, 2015, 12:00:00

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?


More From » jquery

 Answers
7

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/


[#64880] Monday, September 28, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
byrondonavanc questions
;