Ruby on Rails: jQuery datepicker - dates between validation

Posted by Jazz on Stack Overflow See other posts from Stack Overflow or by Jazz
Published on 2012-10-19T09:04:33Z Indexed on 2012/10/19 11:02 UTC
Read the original article Hit count: 253

I have an app that allows a user to create new projects, and the search for them later. One of the options they have when creating a project is giving them start and end dates. At the moment all the code works properly for creating and searching on the dates, but I am now wanting to restrict what dates the user can enter.

I am needing for an error to flag up when the user tries to enter an end date that is before the start date. It's really more for when the user is creating the project. Here is my code so far =>

Application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require jquery.ui.all
//= require_tree .

$(function() {
  $("#project_start_date").datepicker({dateFormat: 'dd-mm-yy'});
});
$(function() {
  $("#project_end_date").datepicker({dateFormat: 'dd-mm-yy'});
});
jQuery(function(){
jQuery('#start_date_A').datepicker({dateFormat: "dd-mm-yy"});
});
jQuery(function(){

jQuery('#start_date_B').datepicker({dateFormat: "dd-mm-yy"});
});

New View:

<div class="start_date" STYLE="text-align: left;">
    <b>Start Date:</b>
    <%= f.text_field :start_date, :class => 'datepicker', :style => 'width: 80px;' %>
  </div> 


<div class="end_date" STYLE="text-align: left;">
    <b>End Date:</b>
    <%= f.text_field :end_date, :class => 'datepicker', :style => 'width: 80px;' %>
  </div> 

Search View:

Start dates between

<%=  text_field_tag :start_date_A, params[:start_date_A], :style => 'width: 80px;' %>


-

<%=  text_field_tag :start_date_B, params[:start_date_B], :style => 'width: 80px;' %></br>

I tried following examples online to get this to work by doing this in the application.js file:

$(function() {
$("#project_start_date,#project_end_date").datepicker({dateFormat: 'dd-mm-yy'});
});

jQuery(function(){
jQuery('#start_date_A,#start_date_B').datepicker({dateFormat: "dd-mm-yy"});
});

But then the script doesn't run. I am new to rails and javascript so any help at all is appreciated. Thanks in advance.

UPDATE: Don't know why my question has been voted to be closed. It's quite simple:

I need an error to flag up when the user tries to enter an end date that is before the start date. How can I do that??

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery