金額を入力してください<input type="text" class="input_price">円
$('.input_price') .focusout(function(){ var str = $(this).val(); str = str.replace(/,/g,''); if (str.match(/^(|-)[0-9]+$/)) { str = str.replace(/^[0]+([0-9]+)/g, '$1'); str = str.replace(/([0-9]{1,3})(?=(?:[0-9]{3})+$)/g, '$1,'); } $(this).val(str); }) .focusin(function() { var str = $(this).val(); str = str.replace(/,/g, ''); $(this).val(str); }) .keyup(function(){ var s = new Array(); $.each($(this).val().split(''), function(i, v){ if( v.match(/[0-9]/gi) ) s.push(v); }); if(s.length > 0) $(this).val( s.join('') ); else $(this).val(''); });
.input_price { text-align:right; color: #0082c3; border:1px solid #0082c3; font-size:14px; padding:3px; margin:0 5px; &:focus { background-color:#E7F3F8; border: 1px solid #1ba1e2; outline: none; } }