2

I've copied and modified a js date validator - from ( http://www.javascriptkit.com/script/script2/validatedate.shtml )

I'm using it as part of a jquery modal form, it doesn't let me get past the first criteria.. even though the value is put in form the datepicker jquery UI

function checkDate( d, n ){
        if ( d.val().length > 0 ) {
            var validformat = /^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity
            if ( !validformat.test( d.value ) ) {
                d.addClass( "ui-state-error" );
                updateTips( n + " must be a valid date." );
                return false;
            } else { //Detailed check for valid date ranges
                var monthfield = d.value.split( "/" )[0]
                var dayfield = d.value.split( "/" )[1]
                var yearfield = d.value.split( "/" )[2]
                var dayobj = new Date( yearfield, monthfield-1, dayfield )
                if ( ( dayobj.getMonth()+1 != monthfield ) || ( dayobj.getDate() != dayfield ) || ( dayobj.getFullYear() != yearfield ) ) {
                    d.addClass( "ui-state-error" );
                    updateTips( "Invalid Day, Month, or Year range detected. Please correct and submit again." + n + " must be a valid date." );
                    return false;
                } 
            }
            return true;
        }
    }

when the value of 12/25/2012 is entered it returns... "Notification Date must be a valid date." from the updateTips( n + " must be a valid date." ); line...

help please...

j-p
  • 3,698
  • 9
  • 50
  • 93

1 Answers1

2

I'm not sure what d is. Is it a DOM node? Then you can't call .val() on it. Is it a jQuery wrapper object? Then accessing its .value property will likely result in undefined, and the stringification of that value does not match your format.

Use some more variables, like

var value = d.val(); // to test length, regex and use in split
…
var parts = value.split("/"); // to get year, month and day part
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • sorry - d is passed in value... the call looks like this: bValid = bValid && checkDate( notification_date, "Notification Date" ); Where notification_date is the field name – j-p Dec 20 '12 at 03:46