www.webdeveloper.com
Results 1 to 4 of 4

Thread: Date Validations

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    3

    Date Validations

    Hello all,

    I'm new to this forum and by no means am I a Javascript expert. I'm looking for a code that I can use to check validation of a date in a text field based on the current date. (i.e. If the user punches in a date that is in the past, I want an alert to pop up.) The date can be manually entered or by simply clicking a calendar icon that pops open a calendar. Also if you can provide something that would do the same but not let the user enter a date in the future, that would be awesome. Thanks in advance for your help.

  2. #2
    Join Date
    Aug 2010
    Location
    Ohio
    Posts
    32
    http://www.w3schools.com/jsref/jsref_parse.asp

    That with some coordination from:

    var today = getDate();

    should do the trick.

  3. #3
    Join Date
    Dec 2005
    Posts
    2,984
    First of all, you want to put something in the text box to show the user what format you expect the date to be in. That allows you to be able to easily check the date if the user manually enters it. I usually put something like DD/MM/YYYY as the "default value" in the text box - when the user clicks on it, this disappears. That way, when it comes time for validation, you can use a simple regexp to check to see if the contents of the text box are of the format :

    /^\d{2}\/\d{2}\/\d{4}$/

    Then you can can use this function that I found somewhere to determine if the date is valid:
    Code:
     function IsValidDate(Day,Mn,Yr){
        var DateVal = Mn + "/" + Day + "/" + Yr;
        var dt = new Date(DateVal);
    
        if(dt.getDate()!=Day){
            return(false);
            }
        else if(dt.getMonth()!=Mn-1){
        //this is for the purpose JavaScript starts the month from 0
            return(false);
            }
        else if(dt.getFullYear()!=Yr){
            return(false);
            }
            
        return(true);
     }
    Should be fairly obvious, it'll return true if the Day, Mn, Yr passed to the function is a valid date.

    If you want to determine if the date is before today, parse the value from the input area like this:
    Code:
    dateArr = document.getElementById('date_input').value.split('/');
    today = new Date();
    //dateArr[1] = month
    //dateArr[0] = day
    //dateArr[2] = year
    passedDate = new Date(dateArr[1]+'/'+dateArr[0]+'/'+dateArr[2]);
    
    if(passedDate < today) {
     alert('This date is in the past!');
    }
    There might be faster methods to do all this stuff, but this is the way I do it.


    EDIT: It is imperative to include all the validation that I showed above IF you are going to allow a user to enter a date string manually.
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

  4. #4
    Join Date
    Aug 2010
    Posts
    3
    Thanks aj nsc. I'm all set!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles