Validating Dates in Javascript

Results 1 to 2 of 2

Thread: Validating Dates in Javascript

  1. #1
    Maureen Co Guest

    Default Validating Dates in Javascript

    Does javascript have a function or a method similar to IsDate in VBScript? If not, can someone give a sample function that validates a date?

  2. #2
    Joseph Hatcher Guest

    Default RE: Validating Dates in Javascript

    Here is the rendition I use.<BR><BR><BR>function IsDate(date_val)<BR>{<BR> re = /(1[0-2]&#1240?[1-9])/(0?[1-9]&#124[12][0-9]&#124 3[01])/([1-9]ddd)/<BR> if (re.test(date_val))<BR> {<BR> month = RegExp.$1;<BR> day = RegExp.$2; <BR> year = RegExp.$3;<BR> max=31;<BR> if (month==2)<BR> {<BR> if ((year%4==0))<BR> {<BR> max=29;<BR> }<BR> else<BR> {<BR> max=28;<BR> }<BR> }<BR> else if (month==4&#124&#124month==6&#124&#124month==9&#124 &#124month==11)<BR> {<BR> max = 30;<BR> }<BR> <BR> if (day&#060;=max)<BR> {<BR> return true;<BR> }<BR> else<BR> {<BR> return false;<BR> }<BR> }<BR> else<BR> {<BR> return false;<BR> } <BR>}<BR><BR>You might have to modify this some for your purposes.<BR>This allows one or two digits for day and month but it has to have 4 digits for the year value. (You might want to accept two digits)<BR>Also it checks for a slash(&#039/&#039) between the values. You might want it to accept a hyphen or spaces or something else.<BR><BR>Also this obviously does not check for written values such as September 3, 1999.<BR><BR>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts