Solved

Allow flexibility with date input

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at 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…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now