This is the client script function for an asp.net validation control.
<script type="text/javascript">
function validateDateControl(sender, args) {
var d = new Date(args.Value);
args.IsValid = (Object.prototype.toString.call(d) === "[object Date]");
if (!args.IsValid) {
sender.innerText = "Client: Invalid date";
}
return args.IsValid;
}
</script>
Immediate Window Results:
d
NaN
Object.prototype.toString.call(d) === "[object Date]"
true
Why is the comparison evaluating True?
Because
new Date('as;dlas;ld,as;dl,as')is still aDateobject.A better way to check date validity is to see that
Date.getTime()does not returnNaN: