Results 1 to 3 of 3

Thread: validate a date ?!?!?

  1. #1

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538

    validate a date ?!?!?

    How (using javascript) can I tell if a typed in date is valid please ?

    Cheers

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  2. #2
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606
    how about that:

    Code:
    function y2k(number) { return (number < 1000) ? number + 1900 : number; }
    
    var reason = '';
    
    function isValidDate (myDate,sep) {
    // checks if date passed is in valid dd/mm/yyyy format
    
        if (myDate.length == 10) {
            if (myDate.substring(2,3) == sep && myDate.substring(5,6) == sep) {
                var date  = myDate.substring(0,2);
                var month = myDate.substring(3,5);
                var year  = myDate.substring(6,10);
    
                var test = new Date(year,month-1,date);
    
                if (year == y2k(test.getYear()) && (month-1 == test.getMonth()) && (date == test.getDate())) {
                    reason = '';
                    return true;
                }
                else {
                    reason = 'valid format but an invalid date';
                    return false;
                }
            }
            else {
                reason = 'invalid spearators';
                return false;
            }
        }
        else {
            reason = 'invalid length';
            return false;
        }
    }
    
    function tellMeIfInvalid(myDate) {
        if (isValidDate(myDate,'/'))
            document.write(myDate + ' = valid date<BR>');
        else
            document.write(myDate + ' = ' + reason + '<BR>');
    }
    
    tellMeIfInvalid('21/02/1999');
    tellMeIfInvalid('2190291999');
    tellMeIfInvalid('2/02/1999');
    tellMeIfInvalid('21/2/1999');
    tellMeIfInvalid('21/02/199');
    tellMeIfInvalid('32/02/1999');
    tellMeIfInvalid('21/02/1999');
    //--></SCRIPT>
    i took it at irt.org

  3. #3

    Thread Starter
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Wow, a nice easy one the
    so while vb has the isdate function, you have to do all that, glad someone has done this before

    Thankyou again Sebs !

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

Posting Permissions

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



Click Here to Expand Forum to Full Width