Skip to content

Instantly share code, notes, and snippets.

@lequinharay
Created December 19, 2012 08:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lequinharay/4335202 to your computer and use it in GitHub Desktop.
Save lequinharay/4335202 to your computer and use it in GitHub Desktop.
フォームの入力値を数値のみに制限するjavascript
/*
フォームの入力値を数値のみに制限するjavascript
web上に落ちてたものから以下の点だけ変えた。
・keyCodeがfirefoxで上手く動かなかったのでwhichを使用
・deleteとか矢印キーによる移動は活かしておきたかった
<input class="no-numeric" style="ime-mode: disabled" />
の様にして使う
*/
$(function(){
$('.no-numeric').keypress(function(e){
if((e.which < "0".charCodeAt(0) || "9".charCodeAt(0) < e.which) && e.which != 8 && e.which != 0) {
return false;
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment