Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (13.1k points)

Can anyone help me how I can able to validate the date and set the age accordingly. I tried with the below code, but it gives alert even if I give the correct date.

var errMessage = "";

function checkForm() {

    validateName();

    validateSurname();

    carSelect();

    validateDOB();

    if (errMessage == "") {

    } else {

        alert(errMessage);

    }

}

...

function validateDOB()

{

    var dob = document.forms["ProcessInfo"]["txtDOB"].value;

    var pattern = /^([0-9]{2})-([0-9]{2})-([0-9]{4})$/;

    if (dob == null || dob == "" || !pattern.test(dob)) {

        errMessage += "Invalid date of birth\n";

        return false;

    }

    else {

        return true

    }

}

Any help would be appreciated.

1 Answer

0 votes
by (26.7k points)

You can use the below pattern to check and validate for the date:

var input = '33/15/2000';

var pattern = /^((0[1-9]|[12][0-9]|3[01])(\/)(0[13578]|1[02]))|((0[1-9]|[12][0-9])(\/)(02))|((0[1-9]|[12][0-9]|3[0])(\/)(0[469]|11))(\/)\d{4}$/;

alert(pattern.test(input));

I hope this will help.

Want to know more about Java? Prefer this tutorial on Learn Java.

Want to become a Java Expert? Join Java Course now!!

Related questions

0 votes
1 answer
asked Jan 28, 2021 in Java by dante07 (13.1k points)
0 votes
2 answers
0 votes
1 answer
asked Dec 4, 2020 in SQL by Appu (6.1k points)
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...