React-datepicker: enable to set date only if it 18 years old and above

All we need is an easy explanation of the problem, so here it is.

I have a form where a user can submit some info needed

One of the fields is a Date of Birth

I am using react-datepicker package for that specific field

A piece of code looks like this:

        <label>
          <DatePicker
            autoComplete='off'
            dateFormatCalendar="MMMM"
            showYearDropdown
            scrollableYearDropdown
            onChange={this.handleChange}
            selected={this.state.formData.dob}
            maxDate={moment().subtract(6570, "days")}
            minDate={moment()}
            placeholderText="Date of birth"
            name="dob"
            customInput={
              <Input type='text' onKeyPress={e => e.preventDefault()} validations={[required]} value={this.state.formData.dob} />
            }
          />
        </label>

6570 = 18*365 are days converted from 18 years, I know it’s a not a solid one, because there are a 364 days also

Here is a screenshot for datepicker not being able to choose a date

https://gyazo.com/4d66a8e59dbca5e79c3af7d6de139d21

Any thoughts or recomendations how can achieve how user can be at least 18 yo to submit a form?

Thank you!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

This issue can be resolved by using Specific date range example.

<DatePicker
    selected={this.state.startDate}
    onChange={this.handleChange}
    minDate={moment().subtract(500, "years")}
    maxDate={moment().subtract(18, "years")}
    showDisabledMonthNavigation
/>

You may check the working demo on codesandbox.io.

Method 2

The solution above wasn’t working for me, I just added ._d after moment().subtract(18, "years") to trim only the date.

my final code:

 <DatePicker
        selected={ selectedDate }
        onChange={ date => setSelectedDate(date) }
        dateFormat='dd/MM/yyyy'
        maxDate={moment().subtract(18, 'years')._d}
        showYearDropdown
        scrollableYearDropdown
      />

Method 3

<DatePicker
    selected={this.state.startDate}
    onChange={this.handleChange}
    dateFormat="dd/MM/yyyy"
    showMonthDropdown
    showYearDropdown
    dropdownMode="select"
    maxDate={subYears(new Date(), 18)}

/>

Method 4

I know I am a bit late for this thread > but recently I also got stuck with this problem
if you want to set a range for date-picker I came up with this solution

            <DatePicker
              showYearDropdown
              dateFormatCalendar="MMMM"
              yearDropdownItemNumber={150}
              minDate={moment().subtract(150, "years")._d}
              maxDate={moment().subtract(18, "years")._d}
              scrollableYearDropdown
            />

Min/Max and yearDropdownItemNumber option need to be added to get rangeLocked response

I hope it will help someone looking for rangeLock

Method 5

If you came here for react native, let me save you some time. The props are:

minimumDate={new Date(moment().subtract(150, "years"))} 

maximumDate={new Date(moment().subtract(18, "years"))}

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply