Solved

Allow flexibility with date input

Posted on 2014-04-13
3
143 Views
Last Modified: 2014-04-14
I have a website with a grid that allows users to input dates.  For one of the columns, the grid brings up a calendar control that users can pick.  The calendar control and column requires that the date be entered in the format dd/mm/yyyy.  The column also allows manual input, but requires that the full 4 digit year be entered.  

The grid allows short months and day (ie no leading zero), but requires that the full four digit year be entered.  I can validate entry into the grid, but need some code to check to see if the year has been entered, and if not, supplement the entry with the current year.

For example:

Entering 4/1/2014 works.

Entering 4/1/14 does NOT work.

Entering 4/1 does NOT work.

So, if the user only enters a two digit year, I want the code to add "20" to the front.  

If the user does not enter a year, I want the code to append the current year.

I would love to add this flexibility to my website.  Thank you in advance.  Brian
0
Comment
Question by:MrBeedge
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
jayakrishnabh earned 250 total points
ID: 39998374
add the below change function for the input columns. Here i have used direct 'txtDate' which is an input id of the control. Please add this function to your input in any of the way you prefer.

$("#txtDate").change(function () {
                var inputDate = $(this).val();
                var arry = inputDate.split("/");

                if (arry[2] == undefined) {
                    inputDate = inputDate + "/" + new Date().getFullYear();
                }
                else {
                    if (arry[2].length == 2) {
                        var abc = "" + new Date().getFullYear();
                        inputDate = arry[0] + "/" + arry[1] + "/" + abc.substr(0, 2) + arry[2];
                    }
                }
                $(this).val(inputDate);
            });
0
 
LVL 22

Assisted Solution

by:Mrunal
Mrunal earned 250 total points
ID: 39998383
hi
here is your solution:

$('#btnValidate').click(function(){    
    ValidateDate();
});

function ValidateDate()
{
    var dateParts = $('#txtDate').val().split('/');    
    var year = dateParts[2];    
    if(typeof(year) == 'undefined')
    {        
        year = (new Date()).getFullYear();        
    }
    else if(year.length == 2)
    {
        year = "20" + year;        
    }
    dateParts[2] = year;
        
    $('#txtDate').val(dateParts[0] + "/" + dateParts[1] + "/" + year);
    
}

Open in new window


HTML is:

<input type="text" id="txtDate" />
<input type="submit" value="Validate Date" id="btnValidate" />

Open in new window


Working Demo:

http://jsfiddle.net/wA3N8/
0
 

Author Closing Comment

by:MrBeedge
ID: 40000854
These both worked for me.  Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question