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

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -