/    Sign up×
Community /Pin to ProfileBookmark

Date comparison format problem

I’m trying to validate that two dates are in the correct order. The “from” <= the “to” date for it to be valid. The code I’m using is:

<script>
function todayDate(from, to)
{
var f = new Date(from);
var n = new Date(to);
if (f <= n)
{
return true;
}
else
{
alert(“Please enter a valid date range.”);
return false;
}

}
</script>

<form action=”xfi_info_lookup_results.asp” method=”get” name=”xfi_info” target=”_blank” id=”xfi_info”
onsubmit=”return todayDate(document.xfi_info.txtShipFromDate.value,document.xfi_info.txtShipToDate.value)”>

…I think the problem is in my date formats, instead of using dd/mm/yyyy I’m using dd-mmm-yyyy (i.e. 20-JAN-2004). I’m actually completing the date fields with a calndar script and modifying the date format with an array function.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@ExuroJan 30.2004 — I added in two regular expressions to reformat your dates:

[size=2]
&lt;script type="text/javascript"&gt;
&lt;!--
function todayDate(from, to)
{
[color=red] from = from.replace(/^([^-]*)-([^-]*)-([^-]*)$/, "$2 $1 $3");
to = to.replace(/^([^-]*)-([^-]*)-([^-]*)$/, "$2 $1 $3");[/color]
var f = new Date(from);
var n = new Date(to);
if (f &lt;= n)
{
return true;
}
else
{
alert("Please enter a valid date range.");
return false;
}
}
//--&gt;
&lt;/script&gt;[/size]
Copy linkTweet thisAlerts:
@heproxauthorJan 30.2004 — Thanks for the help, I've changed my code slightly to validate against leaving the date fields blank, however I can't seem to get the expressions to work correctly, the error tells me the object is not supported? My code is now:

<script>

function validate(from,to)

{

from = from.replace(/^([^-]*)-([^-]*)-([^-]*)$/, "$2 $1 $3");

to = to.replace(/^([^-]*
)-([^-]*)-([^-]*)$/, "$2 $1 $3");

var f = new Date(from);

var n = new Date(to);

if(document.xfi_info.txtShipFromDate.value=="")

{

alert("Please Enter a valid date range.");

return false;

}

if(document.xfi_info.txtShipToDate.value=="")

{

alert("Please Enter a valid date range.");

return false;

}

if (f > n)

{

alert("Please Enter a valid date range.");

return false;

}

else

return true;

}

</script>


form action="xfi_info_lookup_results.asp" method="get" name="xfi_info" target="_blank" id="xfi_info"

onSubmit="return validate(this)">
×

Success!

Help @heprox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...