Wrox Programmer Forums
|
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 2nd, 2003, 08:31 AM
Authorized User
 
Join Date: Aug 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default how to validate a date clientside

Sorry kosla78,
But when I tried your script, it always returned false, no matter if the date entered was within the acceptable range or not. So I tried to enhance it a little with the following but it doesn't work either. Boy this doesn't seem like rocket science but I can't find this anywhere on the web, surly someones done clientside date validation before. Thanks for your response.

function ValiDate(oTextBox)
{
var qtrstart =new Date();
var qtrend=new Date();
var entered=new Date();
qtrstart="8/25/2003";
qtrend="10/25/2003";
entered=oTextBox.value;
if(entered < qtrstart | entered > qtrend)
{
alert("Both 'Assigned Date' and 'Due Date' must range between 8/25/2003 and 10/25/2003");
return false;
}
else
return true;
}
 
Old October 3rd, 2003, 02:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 111
Thanks: 0
Thanked 0 Times in 0 Posts
Default

When you say "it doesn't work" - what do you mean?

Furthermore, this isn't an ASP question - surely you'd be better off asking in a javascript forum?

Cheers
Ken

Microsoft MVP - Windows Server (IIS)
www.adOpenStatic.com
 
Old October 3rd, 2003, 03:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,212
Thanks: 0
Thanked 1 Time in 1 Post
Default

To me this seems a strange way to deal with dates. If you want a date object that represents 8/25/2003 why not just do var qtrstart = new Date(2003, 7, 25); (months are zero-based, but day and year are not!)

Also, there does not seem to be anything to check that oTextBox.Value actually contains a valid date. Surely you must check that first before you can even attempt to determine whether it falls within a certain date range.

I use js objects to handle validation, so although I have code to do what you want, its not easy for me to unpick it into the form you appear to need. For example, here is a snippet that checks for a valid date:
Code:
    var cYEAR = 2, cMONTH = 1, cDAY = 0; // change these for other date formats
    var sValueToTest = this.parent.getValueFromObject(this.formName, this.fieldName);
    var bOK = true;

    this.failureMessage = "";

    if (sValueToTest != null)
    {
        // attempt to convert to a date (assume dd/mm/yyyy format)
        var dtValueToTest;
        var aDate;
        try
        {
            aDate = sValueToTest.split("\/");  // change this for other date formats
            dtValueToTest = new Date(aDate[cYEAR], aDate[cMONTH] - 1, aDate[cDAY]);
            if ( isNaN(dtValueToTest) )
            {
                bOK = false;
                this.failureMessage = this.fieldFriendlyName + " must be a valid date in the format dd/mm/yyyy";
            }
        }
        catch (e)
        {
            bOK = false;
            this.failureMessage = this.fieldFriendlyName + " must be a valid date in the format dd/mm/yyyy";
        }

        // if the date conversion succeeded, check its the actual date that was input
        // (e.g. it will accept 31/2/2003 but will change it to 3/3/2003)
        if (bOK)
        {
            // is it the same year?
            if (aDate[cYEAR] != dtValueToTest.getFullYear())
                bOK = false;

            // is it the same month?
            if (aDate[cMONTH] != 1 + dtValueToTest.getMonth())
                bOK = false;

            // is it the same day?
            if (aDate[cDAY] != dtValueToTest.getDate())
                bOK = false;

            if (!bOK)
                this.failureMessage = this.fieldFriendlyName + " must be a valid date in the format dd/mm/yyyy";
        }
Are you able to modify this to fit your date format and inputs (this covers UK/Euro format dd/mm/yyyy, not US mm/dd/yyyy format)? If so, once you have confirmed that the input is a valid date, all you have to add is:
if (dtValueToTest < qtrstart || dtValueToTest > qtrend) // note the || for OR
{
   alert("blah blah...");

Is this any use to you? I'll be happy to explain any parts that are not clear to you.

rgds
Phil





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to validate date jijish ASP.NET 2.0 Professional 4 December 20th, 2007 07:17 AM
Validate Date sparc ASP.NET 1.0 and 1.1 Basics 1 September 20th, 2005 09:55 AM
How to Validate a Date for present and Future Cute Designer J2EE 1 November 9th, 2004 10:45 PM
Validate Expiration date mariakovacs Classic ASP Basics 4 January 26th, 2004 12:53 PM
validate date javascript lcsgeek Classic ASP Basics 3 October 3rd, 2003 04:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.