3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

looking back this year for my techAdvent Calendar 2019

Day 20

Javascriptで半角カナチェックの正規表現

Last updated at Posted at 2019-12-19

概要

Javascriptで半角カタカナ以外の入力を禁止したときにチェックで使った正規表現のメモ。

詳細

半角カタカナのみを入力可能にする場合の正規表現チェック

// 半角カナ以外を空文字に変換
convertHalfPhonetic(val) {
  return val ? val.replace(/[^ヲ-゚ ]*/g, "") : undefined;
}

半角カタカナ、半角英数字のみを入力可能にする場合の正規表現チェック

// 半角カナ・英数字以外を空文字に変換
convertHalfPhonetic(val) {
  return val ? val.replace(/[^ヲ-゚ A-Za-z0-9]*/g, "") : undefined;
}

まとめ

何かと使いどころがあるものなので、メモとして残しておきたい。
実際に使ったときは、入力項目のblurイベントで起動させて使った。

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?