Solved

Allow flexibility with date input

Posted on 2014-04-13
3
138 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
Comment Utility
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
Comment Utility
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
Comment Utility
These both worked for me.  Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

771 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

14 Experts available now in Live!

Get 1:1 Help Now