2

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!

1 Answer 1

1

The problem is that you are performing a per-keystroke test against a regular expression that will never validate a single keystroke as a valid date.

You will need to modify your validation function to, at a minimum, check for a minimum length before engaging the regular expression match test. If the target string isn't the minimum length necessary for testing, skip the test.

One other option is to implement a different kind of visual cue for an improper date, such as the textbox contents being rendered in a different color for an invalid date (eg, red), but doing so introduces accessibility concerns you may need to address.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for your response David. I did try using a pre-validation to check the length of date field for a minimum length and it does make it much better although it still fails when I add the "else" condition to it. My code now looks like...
@USERdEEP01 Hi...don't see the length check in your code...are you sure you've updated it?

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.