JavaScript ValidateDate UK

marc_butler
marc_butler used Ask the Experts™
on
Hi All,

I have a very simple function to validate a date, but it is in US can anyone change it to UK format for me please.
function ValidateDate(str)
{
	// make sure it is in the expected format
	if (str.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)
		return false;
	
	// remove other separators that are not valid with the Date class
	str = str.replace(/[\-|\.|_]/g, "/");
	
	// convert it into a date instance
	var strDate = new Date(Date.parse(str));
	
	// check the components of the date
	// since Date instance automatically rolls over each component
	var arrDateParts = str.split("/");
		return (strDate.getMonth() == arrDateParts[0]-1 && strDate.getDate() == arrDateParts[1] && strDate.getFullYear() == arrDateParts[2]);
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Year 2008
Top Expert 2008

Commented:

function ValidateDate(str)
{
        // make sure it is in the expected format
        if (str.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0)
                return false;
        
        // remove other separators that are not valid with the Date class
        str = str.replace(/[\-|\.|_]/g, "/");
        
        // convert it into a date instance
        var strDate = new Date(Date.parse(str));
        
        // check the components of the date
        // since Date instance automatically rolls over each component
        var arrDateParts = str.split("/");
                return (strDate.getDate() == arrDateParts[1] && strDate.getMonth() == arrDateParts[0]-1 && strDate.getFullYear() == arrDateParts[2]);
}

Open in new window

Author

Commented:
Hi hielo,

I don't think the problem is the return. Ithink its the str.search(/^\d{1,2}[\/|\-|\.|_]\d{1,2}[\/|\-|\.|_]\d{4}/g) != 0) as the results are false at the other end.

I think the problem is the search string?
Expert of the Year 2008
Top Expert 2008
Commented:
try this:
function ValidateDate(str)
{
        // make sure it is in the expected format
        if (!str.match(/^(\d{1,2})[\/|\-|\.|_](\d{1,2})[\/|\-|\.|_](\d{4})/g) )
                return false;
        var d=RegExp.$1;
	   var m=RegExp.$2;
	   var y=RegExp.$3;
 
        // convert it into a date instance
        var strDate = new Date(Date.parse(m+"/"+d+"/"+y));
        
        // check the components of the date
        // since Date instance automatically rolls over each component
        var arrDateParts = String(m+"/"+d+"/"+y).split("/");
                return (strDate.getDate() == parseInt(arrDateParts[1]) && strDate.getMonth() == parseInt(arrDateParts[0])-1 && strDate.getFullYear() == arrDateParts[2]);
}

Open in new window

Author

Commented:
hielo,

Thank you very much for the help, works like a charm.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial