Có thể sử dụng Event Handling hoặc Event Helper. Tất nhiên là sử dụng Event Helper thì ngắn hơn rồi
1 2 3 4 5 6 7 8 9 10 11 | //when key is pressed in the textbox $("#quantity").keypress(function (e) { //if the letter is not digit then display error and don't type anything if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) { //display error message $("#errmsg").html("Digits Only").show().fadeOut("slow"); return false; } }); |
Mình quen viết thế này hơn:
1 2 3 4 5 6 7 8 9 10 11 12 | //when key is pressed in the textbox $("#quantity").keypress ( function (e) { //if the letter is not digit then display error and don't type anything if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57)) { //display error message $("#errmsg").html("Digits Only").show().fadeOut("slow"); return false; } } ); |
Nguồn: http://roshanbh.com.np/2008/04/textbox-accept-only-numbers-digits.html
