Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How Can i validate age on date field using vanilla javascript

I am trying to vallidate age on date input field, if somone age in 18+ then he can vallid if he isn’t a 18+ from field will be error how can i do that?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The following copies a given birthdate into a new constant b18 and varies it by adding 18 years to it. The comparison with the current date then shows whether the person is 18 years old or not.

// const b contains birth date to be tested
document.querySelector("input").addEventListener("input",function(){

 const b=new Date(this.value);

 const b18=new Date(b.getTime());
 b18.setYear(b.getFullYear()+18);

 // alternatively you could calculate that "critical birthdate", 
 // 18 years ago, that makes you an adult today:
 const d18=new Date();
 d18.setYear(d18.getFullYear()-18);

 console.log(new Date()>b18,d18>b);
})
<input type ="date" value="2003-08-29">

about 4 years ago · Juan Pablo Isaza Report

0

You can do this:

        <input id="age" type="number" />


        let age = document.getElementById('age');
        let minAge = 18;

        age.onchange = () => {
            if(!age.value >= minAge){
                    age.style.border = "1px solid red";
                } else if(age.value >= minAge) {
                    ​age.style.border = "1px solid green";
                }
        }
 ​
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!