Solved

Allow flexibility with date input

Posted on 2014-04-13
3
141 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

680 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