Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a pattern to limit the first four numbers in my input box?

The result should be like this, 2018-00130. The first 4 numbers are year, I wanted to limit the year to 2010 up to 2022.

I have tried to use the input type = number but I can’t limit the numbers since I need the dash sign.
My pattern for now is just like this, pattern="[^a-zA-Z]*" maxlength = "10".

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is a very basic RegEx and quite simple to implement.

Here’s the code:

<form>
  <label for="code">Code:</label><br>
  <input type="text" id="code" name="code" pattern="20(1[0-9]|2[0-2])-[0-9]{5}" maxlength="10" required>
  <input type="submit" value="Submit">
</form>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading