Link to home
Start Free TrialLog in
Avatar of TrialUser
TrialUserFlag for Afghanistan

asked on

validate date field in javascript or jquery

I have a datefield . how can i make sure the date entered is valid date? please help

I already use ajaxmask, that allows only entry of numbers in the format 99/99/9999. on top of this I need to make sure this a valid date that SQL server will accept.
Avatar of nap0leon
nap0leon

Try this:
if ( Object.prototype.toString.call(d) === "[object Date]" ) {
  // it is a date
  if ( isNaN( d.getTime() ) ) {  // d.valueOf() could also work
    // date is not valid
  }
  else {
    // date is valid
  }
}
else {
  // not a date
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nap0leon
nap0leon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
your question has been answered by nap0leon correctly
just to avoid further problems, format your date before sending it to SQL by sending it as YYYYMMDD
without any date separators so you can avoid any single configuration problems between clients and servers
Avatar of TrialUser

ASKER

I tried the isNan but it is not catching :- "11/11/1111" or "99/99/9999".

However in th code when I do (DateTime.Parse("txtDate.text")
then I get an error. Please help.
May be is there a valid data range I can check to see if the date is in that range?

Thanks
thx