-1
 <div class="col-sm-5">
    <script type="text/javascript">
        function validateDate(){
            var selectedDate = $('#form-control datepicker').datepicker('getDate');
            var now = new Date();
            now.setHours(0,0,0,0);
            if (selectedDate < now) {
                // selected date is in the past
            }
        }
    </script>
    <input type="text" name="birth_date" class="form-control datepicker" id="field-1" onsubmit="validateDate()">
</div>

Does not validate the input date using this javascript. want to insert a date which is less than the current date.

vishva8kumara
  • 353
  • 1
  • 3
  • 15
Harish
  • 11
  • 4
  • 2
    I'd suggest tidying your code and then using the code block feature of the question editor to make sure your content displays correctly. Also "does not validate" is not a great problem description. Check out [ask], [help], and [mcve]. – CollinD Apr 25 '17 at 18:53
  • an input doesn't have an onsubmit event – charlietfl Apr 25 '17 at 18:57

3 Answers3

1

If I am reading your question correctly, you would like the JavaScript to prevent form submission when the selectedDate is less than now. To transfer this information, you need to return false; in your JavaScript:

<script type="text/javascript">
 function validateDate(){
  var selectedDate = $('#form-control datepicker').datepicker('getDate');
  var now = new Date();
  now.setHours(0,0,0,0);
  if (selectedDate < now) {
   // selected date is in the past
   return false;
  }
 }
</script>

To get this JavaScript function open, you need a <form> with an onsubmit that returns the value of the JavaScript function, as well as a way to submit the form:

<form onsubmit="return validateDate();">
 <input type="text" name="birth_date" class="form-control datepicker" id="field-1">
 <input type="submit">
</form>
Stephen S
  • 354
  • 1
  • 4
  • 21
  • need to change the id on the javascript then it worked. I want to be able to select a birth date that corresponds from 1 year old to 100 years old (1 century) that is the input must less than the current date of system thus giving me the option to choose not less than 1 century. Can u help me on that javascript? :( – Harish Apr 25 '17 at 19:28
  • @Harish try seeing what you can do with selectedDate.getFullYear() and now.getFullYear() – Stephen S Apr 25 '17 at 19:31
0

input elements don't have a submit event; forms do. Assuming you want your code to run when the user presses return, you need to:

  • Wrap the input in a form element and use the form's onSubmit attribute
  • Return false from your handler when the validation has failed

See JavaScript code to stop form submission

Community
  • 1
  • 1
dashkb
  • 1
0

Check the answer to this question.

JQuery Datepicker onchange event help!

You may want to attach an event/function to date picker to do the validation.

If it is any other field, you may attach onchange or onkeyup event to a validation (the way you have tried to do with onsubmit).

Community
  • 1
  • 1
vishva8kumara
  • 353
  • 1
  • 3
  • 15