LoginSignup
4
5

More than 5 years have passed since last update.

HTMLのフォームで全角数字を半角数字に変換するサンプル

Posted at

HTML

<input type="text" class="convertHalfWidthNumeric" >

JavaScript

$(function(){
    $('input.convertHalfWidthNumeric').on('change', function(){
    var converted = $(this).val().replace(/[0-9]/g, function(s) { // (1)
        return String.fromCharCode(s.charCodeAt(0) - 65248); // (2)
    });
    $(this).val(converted);
});

(1) [0-9]の箇所を変えれば、変換したい全角文字の対象を変えられる。

(2) 半角文字の文字コードから65248番目の文字コードが全角文字になるため、このロジックを使う

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5