JSFiddle - React, Tailwind, and code Playground
by alokswain
HTML
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.js"></script>
<form id="cartform">
<input name="amount" class="amount" type="text" /> <br />
<button> Click Me! </button>
</form>
JavaScript
$(document).ready(function() {
var previouslyValidInput;
$.validator.addMethod('notmorethan', function(value, element) {
alert(previouslyValidInput);
if(previouslyValidInput == undefined){
return true;
} else {
if(value <= previouslyValidInput ){
return true;
} else {
return false;
}
}
}, 'abc.');
$.validator.addMethod('positiveint', function(value) {
return (value > 0) && (value != 0) && (value == parseInt(value, 10));
}, 'Enter a positive integer value.');
$("#cartform").validate({
rules: {
"amount": {
required: true,
positiveint: true,
notmorethan: true
}
},
errorPlacement: function(error, element) {
error.appendTo(element.next());
}
});
$("button").click(function() {
alert("Is form valid" + $("#cartform").valid());
if($("#cartform").valid() == true){
alert("form is valid updating the previously stored val");
previouslyValidInput = $("input.amount").val();
}
return false;
});
});