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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validating a date with the onblur event.

Status
Not open for further replies.

Excko

Programmer
Nov 10, 2003
7
CA
Hello. I need to validate a date in a form using the onblur event. The date needs to be in the mm/dd/yy format.

Here is the code wich would call the function.

<input type=&quot;text&quot; name=&quot;txtDate&quot; onblur=&quot;return checkDate(this.form.txtDate.value)&quot;>


I have tried coding my own function to validate the date, but with no luck so far. I was wondering if I could get some help. It would be very appreciated.

Thank you
 
Have you tried a keyword search? Looked at the FAQs? This question is asked at least weekly...

I suggest searching on &quot;valid date regexp&quot; or something similar

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
I've been looking at FAQs and doing searches on the net and tried to adapt different code to mine, but with no luck so far.
 
from faq216-567

function isLeap(year){
if(year % 400 == 0){
return true;
} else if((year % 4 == 0) && (year % 100 != 0)){
return true
} else return false;
};

function days_in(month, year){
if(month == 4 || month == 6 || month == 9 || month == 11){
return 30;
} else if(!isLeap(year) && month == 2){
return 28;
} else if(isLeap(year) && month == 2){
return 29;
} else return 31;
};

function checkDate(myItem){
var myArrayDate, myDay, myMonth, myYear, myString, myYearDigit;
myString = myItem.value + &quot;&quot;;
if (myString == &quot;&quot; || myString == &quot;mm/dd/yy&quot;){
myItem.value = &quot;mm/dd/yy&quot;;
return true;
}
myArrayDate = myString.split(&quot;/&quot;);

myDay = Math.round(parseFloat(myArrayDate[1]));
myMonth = Math.round(parseFloat(myArrayDate[0]));
myYear = Math.round(parseFloat(myArrayDate[2]));
myString = myYear + &quot;&quot;;
myYearDigit = myString.length;

if (isNaN(myDay) || isNaN(myMonth) || isNaN(myYear) || (myYear < 1) || (myDay < 1) || (myMonth < 1) || (myMonth > 12) || (myYearDigit != 2) || (myDay > days_in(myMonth, myYear))){
alert(&quot;Please check your Date format. (mm/dd/yy)&quot;);
myItem.value = &quot;mm/dd/yy&quot;;
return true;
} else{
return false;
}
};


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Thank you very much. I appreciate it greatly. Everything works fine now
 
I know you've probably already got your answer, but I figure I'd post this as it is what I usually use, and it a fairly short script. This allows the users to type in the date in lots of different formats as well:
Jan 01, 2003
01/01/2003
2003/01/01
etc...
It also checks for valid leap years and all that good stuff.
Code:
function checkForDate(strPassed) {
   var newDate = new Date();
   newDate.setTime(Date.parse(strPassed));
   if (isNaN(parseFloat(newDate.getFullYear()))) {
      return (false);
   }
   return (true);
}
I know it's not mm/dd/yy format, but you could always put code inside the function to change it to that. And it wouldn't be much more than 3 or 4 lines of code.



-kaht

banghead.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top