最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I get my HTML form date input to only allow specific days of the week to be chosen using JavaScript, React and HTML? - St

programmeradmin1浏览0评论

I am trying to get my Forms Date Input to only allow Thursday, Friday, and Saturday to be selected. But I can't seem to find a way to do it.

Is there some JavaScript or HTML code I could use to solve this issue?

Here is my current code

import React from 'react';

const CustomForm = () => {

const addTwoWeeks = function () {

        let today = new Date();
        today.setDate(today.getDate() + 14);

        let dd = today.getDate();
        let mm = today.getMonth() + 1;
        let yyyy = today.getFullYear();

        if (dd < 10) {
            dd = '0' + dd
        }
        if (mm < 10) {
            mm = '0' + mm
        }

        today = yyyy + '-' + mm + '-' + dd;

        return today;
    }
 

return (

<form>

<label htmlFor="date">Pick-Up Date:</label>
<input type="date" id="date" name="date" min={addTwoWeeks()} required />

</form>

)
}

export default CustomForm;

I am trying to get my Forms Date Input to only allow Thursday, Friday, and Saturday to be selected. But I can't seem to find a way to do it.

Is there some JavaScript or HTML code I could use to solve this issue?

Here is my current code

import React from 'react';

const CustomForm = () => {

const addTwoWeeks = function () {

        let today = new Date();
        today.setDate(today.getDate() + 14);

        let dd = today.getDate();
        let mm = today.getMonth() + 1;
        let yyyy = today.getFullYear();

        if (dd < 10) {
            dd = '0' + dd
        }
        if (mm < 10) {
            mm = '0' + mm
        }

        today = yyyy + '-' + mm + '-' + dd;

        return today;
    }
 

return (

<form>

<label htmlFor="date">Pick-Up Date:</label>
<input type="date" id="date" name="date" min={addTwoWeeks()} required />

</form>

)
}

export default CustomForm;
Share Improve this question edited Aug 5, 2021 at 6:34 krichey15 asked Aug 5, 2021 at 3:57 krichey15krichey15 6917 silver badges15 bronze badges 3
  • 1 I don't think you can disable specific weekdays (e.g showing it in a disabled state) from the HTML5 selector itself. You'll need a third-party date picker plugin. The only restrictions you can put on the HTML5 input datepicker are its lower and upper bounds through the min and max attributes. – WSD Commented Aug 15, 2021 at 22:30
  • 1 This one is quite simple to set up reactdatepicker. – WSD Commented Aug 15, 2021 at 22:32
  • 1 You can just have a validator rule and check wether the day is a specific weekday or not and only allow the form to submit if it's an accepted value else add an error message regarding this restriction to inform the users. Why don't you do something like this? If you want to stick with the native datepicker, then there is no other solution. – Christos Lytras Commented Aug 16, 2021 at 10:21
Add a ment  | 

2 Answers 2

Reset to default 2 +25

It's quite simple if you will use datepicker js

 $("#datepicker").datepicker({
      beforeShowDay: function (d) {
         var day = d.getDay();
         return [day != 0 && day != 1 && day != 2 && day != 3];
     },
});

Full working example:

<html>
        <head>
            <title>HTML</title>
            <script src="https://ajax.googleapis./ajax/libs/jquery/1.11.2/jquery.min.js"></script>

            <link rel="stylesheet" href="https://ajax.googleapis./ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" crossorigin="anonymous" referrerpolicy="no-referrer"/>
            <script src="https://ajax.googleapis./ajax/libs/jqueryui/1.11.3/jquery-ui.min.js" crossorigin="anonymous" referrerpolicy="no-referrer" ></script>
            <script>
            $(function () {
                $("#datepicker").datepicker({
                    beforeShowDay: function (d) {
                        var day = d.getDay();
                        return [day != 0 && day != 1 && day != 2 && day != 3];
                    },
                });
            });
            </script>
        </head>
        <body>
            <input type="text" id="datepicker" />
        </body>
        </html>

The <input type="date" /> does not allow disabling specific days of the week. See here (MDN docs).

In that case, you can either

  1. Prevent form submission and showing a proper validation message for the invalid day selection.
  2. Or, use a third-party react date picker/calendar library (or, if you have time, implement a datepicker).

I remend react-dates(Github link) in case you go for option 2. Keep in mind the size of the import though.

<SingleDatePickerWrapper isDayBlocked={isDayBlocked} autoFocus />

Here(Storybook) is a variant/example which blocks day selection for Fridays. Link to the corresponding code(Gtihub) .

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论