Im trying to create an html input tag that accepts only numbers entered in 1 of 2 formats, and reject all other input.
I want to accept numbers in these formats only, including requiring the dashes:
1234-12
and
1234-12-12
note: this is not for dates, but rather legal chapter numbers
Everything I am reading about regex says that the following should work, but it isn't.
<input class="form-control"
type="text"
pattern="^(\d{4}\-\d{2}\-\d{2})|(\d{4}\-\d{2})$"
required />
Devtools Console Error in Chrome:
Pattern attribute value
^(\d{4}\-\d{2}\-\d{2})|(\d{4}\-\d{2})$
is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(\d{4}-\d{2}-\d{2})|(\d{4}-\d{2})$/: Invalid escape