Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit the number of digits in number input?

Tags:

html

input

I am creating a form that will allow a user to enter a birthday with month, day and year. For year, I am trying to write the html code such that it will only allow positive number and also limit it to 4 digits. Here is the code in question:

<input type="number" name="year" maxlength="4"  max="9999" min="0" step="1" placeholder="YYYY" p="[0-9]{4}" style="color:#888;" required/> 

I've scoured the web for a solution to what I am trying to do but every time I try it, it will allow me to enter a number that is greater than 4 digits... basically I am trying to limit the user to enter a valid birth year:

Any advice?? thanks in advance!!

Birth year greater than 4 digits input

like image 936
Brian Lee Avatar asked Oct 15 '25 15:10

Brian Lee


1 Answers

thank you kennytm, your link has answered by question. I just needed to add this

onKeyPress="if(this.value.length==10) return false;
like image 87
Brian Lee Avatar answered Oct 18 '25 07:10

Brian Lee