0

私は、12/24 時間形式のみ (_ _ : _ _ AM/PM なし) の検証を行う方法についてインターネットを精査してきました。要件は、時間を無効にするキー押下を積極的に防止する必要があることです。また、コロンの前に 23 を超えない 1 桁または 2 桁を受け入れる必要があり、59 を超えないコロンの後に 2 桁が必要です。

jquery inputmask と maskedinput の両方を無駄に使用しました。私が何をしようとしても、彼らは正確に正しく動作しません.

私はついにこの記事http://blog.pierrejeanparra.com/2011/10/time-input-mask-regexp-powered/を見つけました。これにはいくつかの優れたロジックが含まれており、正規表現マスクと結び付けることができるほぼ完璧な正規表現が得られます目的の動作を取得します。残念ながら、この式には 1 つだけ小さなバグが残っており、私は頭を悩ませていましたが、理解できませんでした。式は次のとおりです。

/^(([0-1][0-9]|2[0-3]|[0-9])|([0-1][0-9]|2[0-3]|[0-9])(:)[0-5]?[0-9]?)$/

残っている問題は、[0-5] がオプションであるため、1:6 が許可されることです。削除しようとすると?[0-5] の後、: はもう機能しません。助けていただければ幸いです。これは、完全に機能する解決策がないように見える一般的な問題であることを私は知っています。

これは、実証するplnkrです

http://plnkr.co/edit/OE6PGTuCvQa380S7b8Zg?p=preview

4

2 に答える 2

0

この単純なものはどうですか:

^(?:[01]?\d|2[0-3]):[0-5]?\d$

説明:

The regular expression:

(?-imsx:^(?:[01]?\d|2[0-3]):[0-5]?\d$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    [01]?                    any character of: '0', '1' (optional
                             (matching the most amount possible))
----------------------------------------------------------------------
    \d                       digits (0-9)
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    2                        '2'
----------------------------------------------------------------------
    [0-3]                    any character of: '0' to '3'
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  :                        ':'
----------------------------------------------------------------------
  [0-5]?                   any character of: '0' to '5' (optional
                           (matching the most amount possible))
----------------------------------------------------------------------
  \d                       digits (0-9)
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------
于 2013-10-16T08:27:49.027 に答える
0

誰かが興味を持っているなら、ここに答えがありました。

^((([0-1][0-9]|2[0-3]|[0-9])|([0-1][0-9]|2[0-3]|[0-9])(:|h)|([0-1][0-9]|2[0-3]|[0-9])|([0-1][0-9]|2[0-3]|[0-9])(:|h)[0-5][0-9]?))$
于 2013-10-15T20:52:30.010 に答える