简体   繁体   中英

How to make Input Box accept date in format dd/MM/yyyy?

How to make HTML Input Box accept date strictly in format dd/MM/yyyy? Alternatively, any PHP code to pass entered date in this format to MySQL?

The html input field can't be forced to do such a thing. You can either let the user enter what he wants and then check on server side with php if the format matches.

Or you use jQuery and the jQuery Validation plugin (javascript) to already provide client-side a validation before submitting the form. eg like this

$("#myform").validate({
  rules: {
    field: {
      required: true,
      date: true
    }
  }
});

But you still need to check the format server-side as the user could have javascript disabled.

You can use this jquery script to mask the input...

And then use this PHP script:

$date = '10/11/2009'; 
echo date('d-m-Y', strtotime($date)); // outputs 10/11/2009 

...to ensure your input is correct.

The best way to do it is both on the client and server. On the client, JavaScript code would validate that the input is in the right format. You can either write your own validation code or use something like jquery validation . On the server, PHP code would validate the date again before inserting it into the database.

在JS端/[0-3][0-9]/[01][0-2]/\\d{4}/.test(input_value)应该可以完成这项工作。

使用html中的日期,月份和年份的3个下拉菜单......然后将所有3个输入放入php中的数组并使用explode()函数...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM