Demis Schroder Demis Schroder - 6 months ago 5
HTML Question

HTML Pattern numbers and optional - symbol

First question on this forum.
Im trying to make a HTML pattern that requiers numbers (length 1 to 20), and its optional to add a -.

so it should approve 040-123456 and 040123456.

Can anyone help me with this?


You can use this pattern:



  • (?=^\d*-?\d*$): Looks if the string is only composed by digits and one optional dash
  • [\d-]{1,20}: Looks for a string with only digits and dashes, from one to twenty characters