title: Input field allows only numbers and hyphens and within a specific format
body: <p>I have the following code that helps me insure that the input will be only numbers.</p>
  
  <p>However I also need to restrict this number to be within a specific format 0000-00, using sort of alert or maybe return values false or better solution ..</p>
  
  <pre><code>&lt;input type="text" onkeypress='validate(event)' id="class" name="class" maxlength="7" size="15px" placeholder="Ex. 1504-03" required&gt;
  
  &lt;script&gt;
              function validate(evt) {
                var theEvent = evt || window.event;
                var key = theEvent.keyCode || theEvent.which;
                key = String.fromCharCode( key );
                var regex = /[0-9]|\./;
                if( !regex.test(key) ) {
                  theEvent.returnValue = false;
                  if(theEvent.preventDefault) theEvent.preventDefault();
                }
              }
          &lt;/script&gt;
  </code></pre>
  
  <p>Your help for best practices is much appreciated</p>