vendredi 1 juillet 2016

maximum and minimum values in a textbox on keyup


I have want to validate onkeyup event my textbox. i have minimum and maximum value means user can allowed to enter value between range. if less then or greater then i want textbox empty. and i want to generate error to show range e.g 03-15 var timer, min, max; function isValidNumber(v,min,max){ // Check if a valid number is entered return (parseInt(v, 10) >= min && parseInt(v, 10) <= max) ? true : false; } $(document).on('keypress', '.ingredient-percentage', function (evt){ var charCode = (evt.which) ? evt.which : evt.keyCode; if(charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) { return false; } else { var high = parseFloat($(this).data('usage-percent-hi')); var low = parseFloat($(this).data('usage-percent-low')); thisForm = this; // Clear the timer if(timer) clearTimeout(timer); // Set a new timer with a delay of one second timer = setTimeout(function(){ if(isValidNumber(jQuery(thisForm).val(),low,high) == false) { alert('dsa'); var prev = thisForm.next(); if(prev.is('span')) prev.remove(); thisForm.after( "<span class='msg_error'>Range:"+low+"-"+high+"</span>" ); return false; } else { return true }; }, 1000); } }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" name="ingredient-percentage" class="ingredient-percentage" data-usage-percent-hi="15.00" data-usage-percent-low="3.00"> But anyone have other method please suggest me that. Thanks in advence

Aucun commentaire:

Enregistrer un commentaire