2

How do you validate for a valid & empty input type=date field? I want to make sure that my date of birth field should not be = today's date or any date of today. Here are my codes

HTML

<label for="dob">Date of Birth:</label>
<input type="date" name="dob" id="dob"  max="21/11/2017" required/>

JavaScript

else if (dob == "") {
  alert("Please enter a date of birth");
  $("#dob").focus();
  validate = false;
}

The validation for the empty field doesn't work even though I use the same method for the other fields in my form.

Lydia
  • 59
  • 4
  • We need to see more of your code, specifically don't make us guess what that `dob` variable contains. Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/ive-been-told-to-do-a-runnable-example-with-stack-snippets-how-do-i-do-tha)). – T.J. Crowder Nov 21 '17 at 07:25
  • refer this - https://stackoverflow.com/questions/10982386/jquery-how-to-validate-a-date-of-birth-using-jquery-validation-plugin – Tushar Walzade Nov 21 '17 at 07:25
  • And this is the exact scenario for you - http://jsbin.com/inalu3/3/edit?html,output – Tushar Walzade Nov 21 '17 at 07:27
  • Please Refer: https://stackoverflow.com/questions/29445909/to-check-whether-current-date-is-not-less-than-present-date-in-dd-mm-yyyy-format – Nilesh Mahajan Nov 21 '17 at 07:28

2 Answers2

1

The format must be

yyyy-mm-dd

<label for="dob">Date of Birth:</label>
<input type="date" name="dob" id="dob"  max="2017-11-21" required/>

Cross-browser way

Here's an example from developer.mozilla.org https://jsfiddle.net/api/mdn/

-2

You can use typeof($("#dob").val())==='undefined'

  else if (typeof($("#dob").val())==='undefined') {
    alert("Please enter a date of birth");
    $("#dob").focus();
    validate = false;      
}