MAC
MAC

Reputation: 6577

Validation for Date

I have one web application, in this i have to validate one date field of format like mm/dd/yyyy. I searched in the net but i didn't get the proper one. Please help me by providing the new function or by correcting on my code. My code is shown below.. I had called this JS function at onblur event..

function isValidDate() {

var re = new RegExp('^(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/(19|20)dd$');

if (form1.txtDateOfOccurance.value != '' && !form1.txtDateOfOccurance.value.match(re)) {
    alert("Invalid date format: " + form1.txtDateOfOccurance.value);
    form1.txtDateOfOccurance.value = "";
    form1.txtDateOfOccurance.focus();
    isEnable();
    return false;
}

}

Thanks in advance..

Upvotes: 1

Views: 604

Answers (6)

danyim
danyim

Reputation: 1293

This is my implementation. It checks for a valid range and everything.

function validateDate(g) {
    var reg = new RegExp("^(([0-9]{2}|[0-9])/){2}[0-9]{4}$");
    // Checks if it fits the pattern of ##/##/#### regardless of number
    if (!reg.test(g)) return false;
    // Splits the date into month, day, and year using / as the delimiter
    var spl = g.split("/");
    // Check the month range
    if (parseInt(spl[0]) < 1 || parseInt(spl[0]) > 12) return false;
    // Check the day range
    if (parseInt(spl[1]) < 1 || parseInt(spl[1]) > 31) return false;
    // Check the year range.. sorry we're only spanning ten centuries!
    if (parseInt(spl[2]) < 1800 || parseInt(spl[2]) > 2800) return false;
    // Everything checks out if the code reached this point
    return true;
}

Upvotes: 0

Kangkan
Kangkan

Reputation: 15571

You can use:

function checkdate(input){
var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
var returnval=false;
if (!validformat.test(input.value))
alert("Invalid Date Format. Please correct and submit again.");
else{ //Detailed check for valid date ranges
var monthfield=input.value.split("/")[0];
var dayfield=input.value.split("/")[1];
var yearfield=input.value.split("/")[2];
var dayobj = new Date(yearfield, monthfield-1, dayfield);
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Day, Month, or Year range detected. Please correct and submit again.");
else
returnval=true;
}
if (returnval==false) input.select()
return returnval;
}

Upvotes: 0

Tracker1
Tracker1

Reputation: 19334

Here's the regex you want.


var re = /^(0[1-9]|1[0-2])\/(0[1-9]|[1-3]\d)\/((19|20)\d\d)$/

Though you are probably better off, as inkedmn suggests validating by parsing the input, since MM/dd/yyyy is a recognized date format via Date.parse.

Upvotes: 1

anaconda
anaconda

Reputation: 1092

function IsValidDate(str) {
    var str2=""; 
    var date = new Date(str); 
    str2 = (date.getMonth()+1) + "/" 
               + date.getDay() + "/" 
               + (date.getYear()+1900);

    return (str == str2);
}

Upvotes: 0

brettkelly
brettkelly

Reputation: 28205

I'd just try parsing the string as a Date object and check the result (assuming you only need to know if it's a valid date or not):

var myDate = Date.parse(form1.txtDateOfOccurance.value);
if(isNaN(myDate)){ 
    // it's not a real date
}

Upvotes: 0

kb.
kb.

Reputation: 2005

Have a look at http://www.smartwebby.com/DHTML/date_validation.asp, first search result when googling for "javascript date validation"..

Upvotes: 2

Related Questions