Any reason why the following code would not fire on keypress but would on page reload?
$(document).ready(function(){
$('#weight').keypress(valNum("weight", "weight"));
function valNum(number, clas){
var name = $("#"+number).val();
var reg = /^([0-9\-\ \.]{2,6})$/;
if(!reg.test(name)){
$("."+clas+" .nogood").show();
$("."+clas+" .good").hide();
}else{
$("."+clas+" .good").show();
$("."+clas+" .nogood").hide();
}
}
})
Change:
To:
With your previous statement, problem was that
valNumfunction was getting executed immediately because of("weight", "weight"). That’s how you call a JS function immediately egfunc(). However,keyupneeds a callback function which is exactly what we have provided it with in latter case.