I am new to Classic ASP and javascript. I am trying to validate a date to be in mm/dd/yyyy format as soon as the user enters it in a textbox. But everytime I enter two numbers in the date field it fires up the date validation(validateExpFromDt) and says that it is an invalid date. I understand it is something to do with the OnKeyUp event that fires the javascript function but the same event is working for different text field of Account Number though(validateAccNum(this) ).How can I validate only after the user enters the complete date and not just 1 number? Here's the asp snippet
<td width="145" align="left" class="text">
<input type="text" name="txtAccountNum1" value="<%=strPolNum1%>" size='5' align='RIGHT' maxlength="6" onFocus="select()" onKeyUp="return autoTab(this, 6, event), validateAcctNum(this);" tabindex="1" >
</td>
<td width="145" align="left" class="text">
<input type="text" name="txtExpFromDt1" value="<%=strExpFromDt1%>" size='9' align='left' maxlength="10" onFocus="select()" onKeyUp="return autoTab(this, 10, event), validateExpFromDt(this);" tabindex="1" >
</td>
Below is the Javascript Function that is called:
function validateExpFromDt(inputExpFromDt) {
var validformat=/^\d{2}\/\d{2}\/\d{4}$/; //Basic check for format validity
var returnval=false
if (!validformat.test(inputExpFromDt.value))
alert("Invalid Date. Please ensure it is in mm/dd/yyyy format and submit again.");
else{ //Detailed check for valid date ranges
var monthfield=inputExpFromDt.value.split("/")[0]
var dayfield=inputExpFromDt.value.split("/")[1]
var yearfield=inputExpFromDt.value.split("/")[2]
var dayobj = new Date(yearfield, monthfield-1, dayfield)
if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
alert("Invalid Date. Please ensure it is in mm/dd/yyyy format and submit again.")
else
returnval=true
}
if (returnval==false)
inputExpFromDt.select();
return returnval;
}
Thank you so much for your response!