Thursday, September 29, 2011

Date validation using JavaScript

Just to tell you every one I am too bad at javascript !!!
Here is a date validation through javascript that will give an alert message if you try to give a date less than today. It will always take the future date

 var expDate = document.getElementById('txtDate').value;
    var date = expDate.substring(0, 2);
    var month = expDate.substring(3, 5);
    var year = expDate.substring(6, 10);
    var myDate = new Date(year, month - 1, date);
    var today = new Date();

    if (myDate > today) {     
    }
    else {
        alert('Please enter a date greater than today !');
        return false;
    }

1 comment:

  1. Why would you do any of that? It's wrong to assume formatting.

    var date = new Date(document.getElementById('txtDate').value)

    if (isNaN(date.getFullYear()) || date < new Date()) return false;

    ReplyDelete