Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript to validate date???? please help

Status
Not open for further replies.

kuolung

Programmer
Sep 2, 1999
51
US
does anyone have or know javacript that can do validate the date in these 4 formats:
mm/dd/yyyy
mmm dd, yyyy (aug 13, 1999)
mm/yyyy
yyyy

please help!! Thanks!!!!...
 
please read the faq on it
and THEN let us know what's NOT in the faq you wish answered
 
FAQs most links are outdated! please help!
 
i'm really sorry i haven't seen they were unavailable at the moment - and i have to leave now so i can't spend time for a detailed answer
i can suggest you to use the Date object in javascript, its doc (netscape only - but it should fit ie) can be found at :
alternatively you can parse it as if it was a string (using substr and index to retrieve the / or ,)
i hope since tomorrow morning someone will pop by and help you, or maybe you will manage with these 2 ideas (actually one is enough ;]), else i'll be here tomorrow
 
sakura,

Here's my FAQ back: FAQ216-567

Hope this helps ...
 
thank you so much! however, seem like it only checks one format which is mm/dd/yyyy
What should i change or add to make it validates all 4 format scenerios:
mm/dd/yyyy
mmm dd, yyyy (e.g. aug 13, 1999)
mm/yyyy
yyyy

Any advices? please help...i'm just a novice in javascript ! Thankssssssssssssssssssssssss



 
well, here is what i have so far (only for mm/dd/yyyy) Please help me with the other 3 allowable formats :
mm/yyyy
mmm dd, yyyy
yyyy

thanks....

-----


<script>
function validateForm() {
dateCheckMsg = checkDate(document.searchchoice.beg_date_val.value);
if (dateCheckMsg.length > 0) {
alert(dateCheckMsg);
return false;
}
dateCheckMsg = checkDate(document.searchchoice.end_date_val.value);
if (dateCheckMsg.length > 0) {
alert(dateCheckMsg);
return false;
}
}
function checkDate(strDate) {
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var dateSeparator=new Array(&quot;/&quot;,&quot;,&quot;);
var errMsg=&quot;Please use the correct date format for the Publishing Date\n(e.g. mm/dd/yyyy - 4/12/2001)&quot;;
var error=0;

if (strDate.length > 0) {
// Checks if the date uses &quot;/,&quot; to separate the month, day, and year.
if (strDate.indexOf(dateSeparator) != -1) {
strDateArray = strDate.split(dateSeparator);

// Checks if the data contains month, day, and year.
if (strDateArray.length != 3) {
return(errMsg);
}

strMonth = strDateArray[0];
strDay = strDateArray[1];
strYear = strDateArray[2];

// Checks if the values are numeric.
if (isNaN(strMonth) || isNaN(strDay) || isNaN(strYear)) {
return(&quot;The Publishing Date must be a numeric value\n(e.g. mm/dd/yyyy - 4/12/2001)&quot;)
}

intMonth=strMonth;
intDay=strDay;
if (strYear.length < 3) {
intYear=parseInt(&quot;20&quot;+strYear);
}
else {
intYear=parseInt(strYear);
}

if (intMonth > 12 || intMonth < 1) {
errMsg+=&quot;\n - The Month for the Publishing Date is incorrect.&quot;;
error=1;
}

if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intDay > 31 || intDay < 1)) {
errMsg+=&quot;\n - The Day for the Publishing Date is incorrect.&quot;;
error=1;
}

if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
errMsg+=&quot;\n - The Day for the Publishing Date is incorrect.&quot;;
error=1;
}

if (intMonth == 2) {
if (intDay < 1) {
errMsg+=&quot;\n - The Day for the Publishing Date is incorrect.&quot;;
error=1;
}

if (LeapYear(intYear) == true) {
if (intday > 29) {
errMsg+=&quot;\n - The Day for the Publishing Date is incorrect.&quot;;
error=1;
}
}
else {
if (intday > 28) {
errMsg+=&quot;\n - The Day for the Publishing Date is incorrect.&quot;;
error=1;
}
}
}

if (error == 1) {
return(errMsg);
}
}

// Returns an error if date does not user &quot;/&quot; to separate the month, day, and year.
else {
return(errMsg);
}
}

return(&quot;&quot;);
}
</script>

<FORM name=searchchoice METHOD=&quot;post&quot; ACTION=&quot;/search&quot; onsubmit=&quot;return validateForm();&quot;>

<INPUT type=&quot;text&quot; name=&quot;beg_date_val&quot; size=&quot;12&quot; maxlength=&quot;12&quot;>
and <INPUT type=&quot;text&quot; name=&quot;end_date_val&quot; size=&quot;12&quot; maxlength=&quot;12&quot;>

</form>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top