Jump to content

Date Validation


happypete

Recommended Posts

Hi,

 

I'm a complete novice with Javascript. I have a script that validates a date and I'm trying to change the date format from: dd/mm/yyyy to: mm/dd/yyyy

 

function validDate(input)
    {
	    input = input.split('/');
	    if(input.length != 3)return false;

	    for(var counter = 0;counter < 3;counter++)
		    if(input[counter] == '' || isNaN(input[counter]) || input[counter] < 1)return false;
		    else input[counter] = input[counter] * 1;

	    var compare = new Date(input[2], input[1] - 1, input[0]);

	    if(compare.getFullYear() != input[2])return false;
	    if(compare.getMonth()    != input[1] - 1)return false;
	    if(compare.getDate() != input[0])return false;

	    return compare;
    }

Link to comment
https://forums.phpfreaks.com/topic/269432-date-validation/
Share on other sites

solved

function validDate(input)
    {
	    input = input.split('/');
	    if(input.length != 3)return false;

	    for(var counter = 0;counter < 3;counter++)
		    if(input[counter] == '' || isNaN(input[counter]) || input[counter] < 1)return false;
		    else input[counter] = input[counter] * 1;

	    var compare = new Date(input[2], input[0] - 1, input[1]);

	    if(compare.getFullYear() != input[2])return false;
	    if(compare.getMonth()    != input[0] - 1)return false;
	    if(compare.getDate() != input[1])return false;

	    return compare;
    }

Link to comment
https://forums.phpfreaks.com/topic/269432-date-validation/#findComment-1385019
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.