Imran Hasan Imran Hasan - 6 months ago 22
Javascript Question

Allow numbers only after some alphabet?


I need to make sure that a certain field only takes numbers as after some default alphabetic characters Like XXX-XX-1234. Here XXX-XX- are default characters which shouldn't be removed. After these alphabets I want the user to be unable to type in any characters other than numbers.



Is there a neat way to achieve this?

Answer

You can do with input regex pattern too

<input type="text" name="my_name"
  pattern="[A-Za-z]{3}-[A-Za-z]{3}-[A-Za-z]{3}-[0-9]{4}" title="my title">

in your case (removing the number)

 <input type="text" name="my_name" value="ABC-DE" 
  pattern="[A-Za-z]{3}-[A-Za-z]{2}-" title="my title">
Comments