Home > Enterprise >  React reactstrap date form age 18 or older
React reactstrap date form age 18 or older

Time:03-02

I am working on a project that has a form. I am using Reacttsrap for it. One of the fields is a date for the user. I need to use a logic that will allow users with age 18 or older. Also, disable future dates. There isn't much explanation about it in the reactstrap documentation. Also, I have seen many examples online, but I haven't seen one with these requirements and make it work. Currently, I have this code of the form:

<FormGroup>
    <Input 
        id='user-age' 
        name='date' 
        type='date'
        aria-label='Date of birth'
        value={this.state.date}
        // onChange={(event) => this.handleInputChange(event)} 
    />
    <Label for='user-age'>*Date of birth (DD/MM/YY)</Label>
</FormGroup>

The commented code is just an idea of a handler to make it work. Any help would be much appreciated it.

CodePudding user response:

I just found an answer from here that it helped me solve the issue.

  function getAge(dateString: string | number | Date) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}
  • Related