title: Allow only number and a single hypen between number
body: <p>I am new to <code>regular expression</code>. I am looking/searching for a restriction that only allow a single hypen between numbers. I have found a sample formula of regex but I don't know why my code below still accepts a character.</p>
  <p>What I want to achieve is</p>
  <p><strong>Output</strong></p>
  <blockquote>
  <p>1-1</p>
  </blockquote>
  <p>something like that.</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>    $('#txt').keypress(function () {
          const exp = /^\d{1,2}(-\d{1,2})?$/;
          const el = $(this);
          if (exp.test(el.val()) == true)
              alert("a");
      });</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;input type="text" id="txt"&gt;</code></pre>
  </div>
  </div>
  </p>