47
45

More than 5 years have passed since last update.

【jQuery】inputの全角数字・全角アルファベットを半角に変換

Last updated at Posted at 2014-09-11

便利なのでメモ。

DEMO

index.html
<!DOCTYPE html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="jquery.js"></script>
</head>
<html lang="en">
<body>
    <div>
        <p><input type="text" class="js-characters-change"></p>
    </div>
</body>
</html>
jquery.js
$(function(){
    $(".js-characters-change").blur(function(){
        charactersChange($(this));
    });


    charactersChange = function(ele){
        var val = ele.val();
        var han = val.replace(/[A-Za-z0-9]/g,function(s){return String.fromCharCode(s.charCodeAt(0)-0xFEE0)});

        if(val.match(/[A-Za-z0-9]/g)){
            $(ele).val(han);
        }
    }
});
47
45
1

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
47
45