Solved

Allow flexibility with date input

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

751 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