2017-10-28 90 views

回答

0

\d{5}意味着5位是必需的, (-\d{4})?意味着字符 「 - 」 和其他4位是可選

0
pattern="\d{5}(-\d{4})?" 

手段

  1. 開始以5個數字
  2. 其次一個破折號(可選)
  3. 後跟4位數字(可選)

48412 - Accepted 
4488 - Rejected 
44775-4412 - Accepted 
74547-45745 - Rejected 

規則如下

^  a string that starts with... 
(  either 
    \d a digit (0-9)... 
    {4} that repeats four times... 
|  or 
    \d a digit (0-9)... 
    {6} that repeats six times... 
) 
$ and then ends