Validate Date Input

[Total: 0    Average: 0/5]

This script validates a string to be a date literal.

Head Code:

<script>

function checkMyDate(_form) {
    var _value = _form.myDate.value;
    var _test = Date.parse(_value)

    if (isNaN(_test)) {
        _form.myDate.value = "";
        alert("Not a valid date!");
        _form.myDate.focus();
    }
    else {
        nDate = new Date(_test);
        _form.myDate.value = nDate.toUTCString();
    }
}

</script>

Body Code:

Example: &quot;10/01/2001&quot;
<form name="checkDateForm">
<input size=30 name="myDate" value="12/29/2002">
<input type="button" value="Check Date" onclick="checkMyDate(this.form)">
</form>
<script>
onload=function(){document.checkDateForm.myDate.focus();}
</script>

Times Viewed: 1

Leave a Reply

Your email address will not be published. Required fields are marked *