LoginSignup
0
0

More than 1 year has passed since last update.

[正規表現]小数値が指定のケタ数・指定の範囲内を判定する

Last updated at Posted at 2023-01-29

要件(やりたいこと)

  • 小数値で0~99.999の範囲かを判定したい。
    • 実現方法は問わないが、今回は正規表現を使うことにした。
  • 数字は半角数字のみ。全角はNG。
  • 小数は第3桁まで。

要件の例示

  • OK例
    • 0, 0.1, 0.01, 0.001, 99, 99.9, 99.99, 99.999
    • これも許容してOKとする。0.0, 0.00, 0.000
  • NG例
    • 3桁以上の数
      • 100, 1000
    • 小数第4桁以上の数
       * 0.0001, 99.9999, 0.0000
    • 負の数
      • -1
    • 数字以外の文字
      • , 日本
    • 全角数字
      • , 0。1
    • .から始まる数字
      • .99
    • 0から始まる数字
      • 00,000,01, 001

Code

Javascriptで定義して、使用する画面でtoThreeDecimalPlaces.test(value)をするイメージ。

const toThreeDecimalPlaces = /^(0|[1-9]\d{0,1})(\.\d{1,3}|)$/;
0
0
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
0
0