javascript - Negative values not allowed -
what best way prevent users entering negative values in input text element?
currently checking field value on blur, hoping has better solution.
$(".payment").blur(function() { var payment = getamount($(this).val()); if(!isnan(payment) && amount >= 0) { $(this) .css("color", "black") .val(currency(payment)); } else { if(amount < 0) showmessage("negative amounts not allowed", "error"); $(this).css("color", "red"); } }); function getamount(stramount) { var amount = new string(stramount).replace(/\$/g, "").replace(/,/g, ""); return parsefloat(amount); }
you use jquery's .keypress() , prevent default action - key. example: http://jsfiddle.net/5cgxg/
$("#target").keypress(function(event) { if ( event.which == 45 || event.which == 189 ) { event.preventdefault(); } });
Comments
Post a Comment