Justin
Justin

Reputation: 3

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".

Upvotes: 0

Views: 29

Answers (2)

HanKBeatz
HanKBeatz

Reputation: 162

Try this:

<input type="text" maxlength="4" style="width:50px;"> - <input type="text" maxlength="6">

Upvotes: 0

technophyle
technophyle

Reputation: 9128

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>

Upvotes: 1

Related Questions