Wrox Programmer Forums
|
Classic ASP Basics For beginner programmers starting with "classic" ASP 3, pre-".NET." 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 Basics 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 1st, 2003, 07:58 PM
Authorized User
 
Join Date: Aug 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default validate date javascript

I'm very frustrated trying to accomplish a date validation using javascript. I currently am using the following which does not work.
Would someone be so kind as to tackle this for me? Much appreciated.



function ValiDate(oTextBox)
{
if(oTextBox.value < 8/25/2003 || oTextBox.value > 10/25/2003)
{
alert("Both 'Assigned Date' and 'Due Date' must range between 8/25/2003 and 10/25/2003");
return false;
}
else
return true;
}


.....
<input name="dtAsignedDate" type="text" id="dtAsignedDate" size="10" maxlength="10" value='10/1/2003' onChange="return ValiDate(document.frmAssign.dtAsignedDate)">


 
Old October 2nd, 2003, 04:39 AM
Authorized User
 
Join Date: Jul 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, you 're not exactly in the right forum but.. check the code below. In order to compare the value of the text box with a Date, you must define it as a Date object (var now=new Date();).. and thats it!!
The example is working just fine

Cheers
Kostas Lagos
Code:
<script language="Javascript">
function ValiDate(oTextBox)
{
var now=new Date();
var ever=new Date();
now="08/25/2003";
ever="10/25/2003";
if(oTextBox.value < now | oTextBox.value > ever)    
{
alert("Both 'Assigned Date' and 'Due Date' must range between 8/25/2003 and 10/25/2003");
return false;
}
else    {
alert(oTextBox.value);
return true;
    }
}
</script>
</head>
<body>
<form name="frmAssign" method="post">
<input name="dtAsignedDate" type="text" id="dtAsignedDate" size="10" maxlength="10" value='10/1/2003' onChange="return ValiDate(document.frmAssign.dtAsignedDate);">
<input name="dtAsigned" type="text" id="dtAsigned" size="10" maxlength="10" value='10/1/2003'>
</form>

 
Old October 2nd, 2003, 08:26 AM
Authorized User
 
Join Date: Aug 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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, 04:07 PM
Authorized User
 
Join Date: Aug 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I know, no one in this forum cares, but just in case you use this area as a general purpose forum, here ya go, works like a champ.

function ValiDate(oTextBox)
{
//the values of these two strings are populated via an asp
//script that pulls start date and end date from my db table
var sd="8/25/2003"
var ed="10/25/2003"

//s,e and i are arrays ([2]=yyyy,[0]=mm,[1]=dd)
//s being the start date array
//e being the end date array
//i being the user input array
var s=sd.split('/')
var e=ed.split('/')
var i=oTextBox.value.split('/')

//create new date objects using constructed arrays
var startdate =new Date(s[2],s[0],s[1]);
var enddate=new Date(e[2],e[0],e[1]);
var inputdate=new Date(i[2],i[0],i[1]);

//run the comparison
if(inputdate.getTime() < startdate.getTime() || inputdate.getTime() > enddate.getTime())
{
alert("Both 'Assigned Date' and 'Due Date' must range between 8/25/2003 and 10/25/2003");
return false;
}
else
return true;
}





Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate Radiobuttonlist using Javascript roseforever C# 1 November 6th, 2008 04:55 AM
Javascript to validate a textbox abhishekkashyap27 Javascript 9 May 6th, 2008 02:41 AM
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
Validate Expiration date mariakovacs Classic ASP Basics 4 January 26th, 2004 12:53 PM





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