onKeyUp on input Date

Asked

Viewed 110 times

1

I need to make a onKeyUp in input Date so that the user cannot choose a date later than the field cut-off date.

Type:

<input type='date' id='data' name='data' max="<?php echo date('Y-m-d'); ?>" required />

Won’t do any good onkey down. It needs to be during the typing of the field and not in the choice in the combo.

Does it have to block typing in the text box with Html5?

But then the user can withdraw...

It’s hard.

  • 2

    Everything done on the front end can be changed. The important thing is to validate the information on the server.

  • ok, but even if changed, is there a way to deny the user who decides to type in the combo instead of choosing the date in the combo to choose a posthumous date? It may be inertial to validate the date with jQuey/Javascript and clear the date if it is higher

  • I never let the user type. I always put the option to choose the date through a Datepicker.

  • I had put an answer with onkeydown='event.preventDefault();', but since you said it didn’t fit, I removed the answer.

  • 1

    Remember that even if the user type a value out of proper the input becomes invalid. Invalidation of the field works, only it only prevents the form from being sent and not the date being prevented from being typed. Anyway, without validation on the server, it is useless to validate on the front end.

1 answer

0

Does it have to block typing in the text box with Html5?

Yes. With the attribute readonly.

<input type='date' id='data' name='data' max="<?php echo date('Y-m-d'); ?>" readonly />
  • The readonly blocks the field, at least in Opera.

  • True, blocks the whole field including popup with calendar

Browser other questions tagged

You are not signed in. Login or sign up in order to post.