0

I am using jQuery datepicker, I have two datepicker input fields, like following:

<html>
<body>
    <form method=post>
        <input type=text id='start_date'> //datepicker field
        <input type=text id='end_date'>  // datepicker field
        <input type=submit value=Submit>
    </form>
</body>
</html>

I would like to implement the feature that, the form submit button is disable unless both datepicker input fields have selected date value from datepicker calendar.

That's if both date fields have selected date value, the button is enabled automatically. How to implement this?

2 Answers 2

1

havent tried to see if this works but something like this if you have jquery

NEW answer : added a new script.

<script type="text/javascript">
         $(document).ready(function(){
             $("#sumbit").disabled("disabled", "disabled");

             var startDate = "";
             var endDate = "";
             $("#start_date").datepicker({
                 onSelect: function(dateText, inst) { 
                    startDate = dateText;
                    if(endDate != ""){ ValidateDates(); }
                 }
             });
             $("#start_date").datepicker({
                 onSelect: function(dateText, inst) { 
                    endDate = dateText;
                    if(startDate != ""){ ValidateDates(); }
                 }
             });

             function ValidateDates(){
                 //Validate the dateformats and remove the attribute
             };

         });
</script>
<form method=post>
    <input type=text id='start_date' class="valDate"> //datepicker field
    <input type=text id='end_date' class="valDate">  // datepicker field
    <input type="submit" value="Submit" id="sumbit">
</form>

OLD answer :

<script type="text/javascript">
         $(document).ready(function(){
             $("#sumbit").disabled("disabled", "disabled");
             $(".valDate").change(function(){
                if($("#start_date).val() != "" && $("#end_date").val() != ""){
                   //validation logic to see if the date is valid
                   $("#sumbit").removeAttr("disabled");
                }
            else{
               $("#sumbit").disabled("disabled", "disabled");
            }
         });
     });
</script>
<form method=post>
    <input type=text id='start_date' class="valDate"> //datepicker field
    <input type=text id='end_date' class="valDate">  // datepicker field
    <input type="submit" value="Submit" id="sumbit">
</form>
Sign up to request clarification or add additional context in comments.

3 Comments

I think you meant to use .change() and not .changed() there.
No, change is not working with jQuery datepicker input fields, I tried. That's why I ask
thanks for pointing that out andyb, Mellon I've updated my answer a little.
1

Change the mark-up of the "submit" to <input type="submit" value="Submit" disabled="disabled"/> and then remove the disabled property in JavaScript when both date fields have been selected. Note that users with JavaScript disabled will get a different experience so you should protect against this in the code that receives the form.

Edit: Added a demo for you - http://jsfiddle.net/acxDU/ :)

Edit 2: Sorry I misread the question and the first demo didn't work with the datepicker. Here is an updated demo that works with the jQuery datepicker http://jsfiddle.net/acxDU/2/

2 Comments

I will only add a readonly attribute to both input fields to make sure both of them have a real date value: jsfiddle.net/marcosfromero/DjzEz
Make sure you don't break accessibility. You should be able to enter a date without JavaScript being turned on. Also, some users will find it easier to just type a date (it's quicker as well!). Simply indicate the correct date format and users can follow that example. Just remember to protect your inputs on the server side :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.