I would like to implement a mask for a text input
field which accepts a date. The masked value should display directly inside of the input
.
Something like this:
<input type='text' value='____/__/__'>
I wrote the mask as a value in that example, but my intent is to allow people to write a date without typing /
or -
to separate months, years and days. The user should be able to enter numbers into the displayed field, while the mask enforces the format automatically as the user types.
I have seen this behavior on other sites, but I have no idea how it works or how to implement it myself.
Below i describe my method. I set event on input in
input
, to call Masking() method, which will return an formatted string of that we insert ininput
.Html:
JQ: Here we set event on input:
JS: Function, which will format string by pattern;