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...

5
  • 1
    12/25/2012 is a valid date ? Commented Dec 20, 2012 at 3:36
  • 2
    ^ The end of the world is before that, so I'd say no, it's not. Commented Dec 20, 2012 at 3:38
  • Try my answer to this question stackoverflow.com/questions/11218181/… Commented Dec 20, 2012 at 3:39
  • @elclanrs - i'm confused by what u r returning in that function... Commented Dec 20, 2012 at 4:25
  • @jpmyob: it returns true or false if the date is valid/invalid. Commented Dec 20, 2012 at 4:29

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.