ps__
ps__

Reputation: 365

Validate date range with jquery datepicker

Im trying to get the values from my datepicker into a new script which should validate that the second chosen value from datepicker is not before the first chosen datepicker.

However, my script won't execute.

This is my view:

 <div class="editor-field">
        @Html.TextBox("PlannedDate", Model.PlannedDate, new { @class = "date", @readonly = "readonly" })
        @Html.TextBox("CompletedDate", Model.CompletedDate, new { @class = "date", @readonly = "readonly" })
        @Html.ValidationMessageFor(model => model.CompanyName)
    </div>
 <input id="submitfloat" type="submit" value="Go" />

This is my Script

  <script>
  $(".date").datepicker();
  $('#submitfloat').submit(function() {
  var fromDate = $('#PlannedDate').val();
  var EndDate = $('#CompletedDate').val();
  if (Date.parse(fromDate) > Date.parse(EndDate)) {
    alert("Date error")
    }
    return false;

});

However, when I start debug ut Firebug, it seems like my line $('#submitfloat').submit(function() { wont execute.

Anyone know what my problem is and how it can be solved? Thanks in advance

Upvotes: 0

Views: 14259

Answers (1)

bernybaeza
bernybaeza

Reputation: 36

well my first answer, i hope it help you.

first my view:

    <script src="Js/jquery.min.js" type="text/javascript"></script>
    <script src="Js/jquery.validate.min.js" type="text/javascript"></script>
    <script src="Js/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script>
    <link href="Css/start/jquery-ui-1.8.18.custom.css" rel="stylesheet" type="text/css" />

i use that for the date picker and the validation

    <form id="custom" action="">
         <input type="text" id="fechaLlegada" name="fechaL" style="color: #4080BF; font: bold 12px verdana; padding: 0 2px 3px 2px;"/> 
         <input type="text" id="fechaSalida" name="fechaS" style="color: #4080BF; font: bold 12px verdana; padding: 0 2px 3px 2px;"/>
         <br />
         <input type="submit"/>
    </form>

now the script

    <script type="text/javascript">
jQuery.validator.addMethod("isValid", function (value, element) {
    var startDate = $('#fechaLlegada').val();
    var finDate = $('#fechaSalida').val();
    return Date.parse(startDate) < Date.parse(finDate);
}, "* End date must be after start date");

$(function () {
    $('#fechaLlegada,#fechaSalida').datepicker()

    $('#custom').validate({
        rules: {
            'fechaL': { required: true, date: true },
            'fechaS': { required: true, date: true, isValid: true }
        }, messages: {
            'fechaL': { required: 'date required msj!',
                date: 'invalid date format msj'
            },
            'fechaS': { required: 'date required msj!',
                date: 'invalid date format msj',
                isValid: 'End date must be after start date'
            }
        }
    });

});
</script>

i added a method to the jquery validate for the date Validation then i use it as a rule in the validation script

this is working for me, i hope it helps someone

Upvotes: 2

Related Questions