Input mask in type=”text”

There is a field in the database with the date type. Those. data is entered there in the following format: 2010-10-10.
There is a <input type="text" name="date_vipis" id="date_v" />field where the date is entered. How to set an input mask so that the data enters the database not on 2010.10.10 or 10.10.2010, but in a format accessible to the database? How to check in Javascript that the field is filled in correctly? jQuery is not recommended. You need javascript and php.
Maybe it’s possible to change the storage format in the data type in the db? If so, how? But the main question is with checking the filling of the field.


Answer 1, authority 100%

Several options:

  • the first one is – input to be replaced by 3 selects, in which the year, month, day should be set in the required order. Next, collect the received data into a date in the desired format, first, of course, checking them in php for correctness;

  • second – pick javascript, for example, from here (date validation) and then check them in php in the same way on right.

After all, no one guarantees that your form will be sent to your server by your script.


Answer 2

Formatting input is easily done with jquery. There is only a minus that (and this is rare, but it happens) js is disabled for some and you will have to additionally check the data on the server. Maybe it’s better to just check on the server and not do extra work?!


Answer 3

I don’t see the point in JS Date Validation. IMHO, better through Masked Input Pluginfor jQuery.


Answer 4

you can also set a mask:

<input type="text" name="date" value="YYYY-MM-DD">

with this filling, YYYY-MM-DD will be automatically entered in the input field
And there you can figure out where YYYY-year MM-month DD-day is possible


Answer 5

In a handler on the server

In the post, the entered string with the date comes. You can set your own date format.

$mydate = date("Y-m-d H:i:s", strtotime($_POST['date']));