Date validation with javascript

P

pete0085

I found some sample code through some online searches for date validation
with javascript. Not really sure what I'm doing, but I have two dates I want
to validate. Basically a "to date" and "from date". The problem I'm having
is the script only works one one of the dates and not both.

Can someone help me with this if I post the code I'm using in a separate post?
 
R

Ronx

Nobody can help if you don't post the code. Better still, publish the
entire page and post a link -we will need to see how the code is being
called, as well as the javascript itself.
--
Ron Symonds
Microsoft MVP (Expression)
http://www.rxs-enterprises.org/fp

Reply only to group - emails will be deleted unread.
 
P

pete0085

:
I will post the entire page. I am not sure how to link it. It's stored on
a shared folder on the network.

<html>

<head>

<script language = "Javascript">
/**
* DHTML date validation script. Courtesy of SmartWebby.com
(http://www.smartwebby.com/dhtml/)
*/
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=2009;
var maxYear=2050;

function isInteger(s){
var i;
for (i = 0; i < s.length; i++){
// Check that current character is number.
var c = s.charAt(i);
if (((c < "0") || (c > "9"))) return false;
}
// All characters are numbers.
return true;
}

function stripCharsInBag(s, bag){
var i;
var returnString = "";
// Search through string's characters one by one.
// If character is not in bag, append to returnString.
for (i = 0; i < s.length; i++){
var c = s.charAt(i);
if (bag.indexOf(c) == -1) returnString += c;
}
return returnString;
}

function daysInFebruary (year){
// February has 29 days in any year evenly divisible by four,
// EXCEPT for centurial years which are not also divisible by 400.
return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 ==
0))) ? 29 : 28 );
}
function DaysArray(n) {
for (var i = 1; i <= n; i++) {
this = 31
if (i==4 || i==6 || i==9 || i==11) {this = 30}
if (i==2) {this = 29}
}
return this
}

function isDate(dtStr){
var daysInMonth = DaysArray(12)
var pos1=dtStr.indexOf(dtCh)
var pos2=dtStr.indexOf(dtCh,pos1+1)
var strMonth=dtStr.substring(0,pos1)
var strDay=dtStr.substring(pos1+1,pos2)
var strYear=dtStr.substring(pos2+1)
strYr=strYear
if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
if (strMonth.charAt(0)=="0" && strMonth.length>1)
strMonth=strMonth.substring(1)
for (var i = 1; i <= 3; i++) {
if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
}
month=parseInt(strMonth)
day=parseInt(strDay)
year=parseInt(strYr)
if (pos1==-1 || pos2==-1){
alert("The date format should be : mm/dd/yyyy")
return false
}
if (strMonth.length<1 || month<1 || month>12){
alert("Please enter a valid month")
return false
}
if (strDay.length<1 || day<1 || day>31 || (month==2 &&
day>daysInFebruary(year)) || day > daysInMonth[month]){
alert("Please enter a valid day")
return false
}
if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
return false
}
if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr,
dtCh))==false){
alert("Please enter a valid date")
return false
}
return true
}

function ValidateForm(){
var dt=document.frmSample.date
var dt=document.frmSample.date1
if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
}


</script>



<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>SMW VACATION Form</title>
</head>

<body bgcolor="#FFFFFF" style="text-align: left; background-attachment:fixed">
<form name="frmSample" method="POST" action="--WEBBOT-SELF--"
onSubmit="return ValidateForm()">

<!--webbot bot="SaveResults" U-File="C:\Documents and
Settings\toddp\Desktop\_private\form_results.csv" S-Format="TEXT/CSV"
S-Label-Fields="TRUE" -->
<p><b><font size="4">
<img border="0" src="file://Smwsvr1/Shared_Data/HTML_Images/logo_small.jpg"
width="146" height="55"> </font><font face="Comic Sans
MS" size="4">SMW VACATION
FORM</font></b></p>
<hr color="#000000" align="left">
<p><b><u>Employee Information:</u>
</b> </p>
<table border="1" width="72%" id="table1" style="border-width: 0px">
<tr>
<td width="175" style="border-style: none; border-width: medium"><b>
<font size="2">OnBase User ID</font></b></td>
<td width="286" style="border-style: none; border-width: medium">
<input type="text" name="OBKey__152_1" size="12">
<input type="submit" value="Autofill" name="obbtn_ks108"></td>
<td style="border-style: none; border-width: medium"><b><font size="2">
Employment Status</font></b></td>
<td style="border-style: none; border-width: medium"
width="235"> <b><font size="2"><input type="text" name="OBKey__153_1"
size="16" readonly></font></b></td>
</tr>
<tr>
<td width="175" style="border-style: none; border-width: medium"><b>
<font size="2">Employee Name </font></b></td>
<td width="286" style="border-style: none; border-width: medium">
<input type="text" name="OBKey__146_1" size="35" readonly>
</td>
<td style="border-style: none; border-width: medium"><b><font size="2">
Hire Date </font></b></td>
<td style="border-style: none; border-width: medium" width="235"> <input
type="text" name="OBKey__147_1" size="16" readonly></td>
</tr>
<tr>
<td width="175" style="border-style: none; border-width: medium"><b>
<font size="2">Department</font></b></td>
<td width="286" style="border-style: none; border-width: medium">
<input type="text" name="OBKey__150_1" size="17"
readonly> </td>
<td style="border-style: none; border-width: medium"> </td>
<td style="border-style: none; border-width: medium" width="235">
</td>
</tr>
<tr>
<td width="175" style="border-style: none; border-width: medium">
</td>
<td width="286" style="border-style: none; border-width: medium">
</td>
<td style="border-style: none; border-width: medium"> </td>
<td style="border-style: none; border-width: medium" width="235">
</td>
</tr>
<tr>
<td width="175" style="border-style: none; border-width: medium">
<font size="2"><b>Start Date
</b></font></td>
<td width="286" style="border-style: none; border-width: medium">
<font size="2"><b>

<p>
<input type="text" name="date" maxlength="10" size="15" tabindex="0">
<font color="#CC0000">(mm/dd/yyyy)</font> </p>

<td style="border-style: none; border-width: medium"><b>
<font size="2" color="#FF0000">
<a target="_blank"
href="file://Smwsvr1/Shared_Data/SMW%20Forms/2009%20Federal%20Holiday%20Calendar.htm">
2009 Calendar</a></font></b></td>
<td style="border-style: none; border-width: medium" width="235">
</td>
</tr>
<tr>
<td width="175" style="border-style: none; border-width: medium">
<font size="2"><b>End Date</b></font></td>
<td width="286" style="border-style: none; border-width: medium">
<font size="2"><b>
<input type="text" name="date1" maxlength="10" size="15" tabindex="0">
<font color="#CC0000">(mm/dd/yyyy)</font></td>
<td style="border-style: none; border-width: medium"><b>
<font size="2">
<a target="_blank"
href="file://Smwsvr1/Shared_Data/SMW%20Forms/2010%20Federal%20Holiday%20Calendar.htm">
2010 Calendar</a></font></b></td>
<td style="border-style: none; border-width: medium" width="235"> </td>
</tr>
</table>
<p><br>
<b>Full Vacation Day </b><input type="radio" value="V1" checked name="R1">
<b>Half Vacation Day</b> <input type="radio" name="R1" value="V2"><br>
<br>
</p>
<p><b><font color="#FF0000">Vacation Status</font></b>
<input type="text" name="OBKey__148_1" size="20" readonly> </p>
<p><b>By:</b> <input type="text" name="OBKey__154_1" size="30" readonly>
<b>On</b>
<input type="text" name="OBKey__155_1" size="20" readonly></p>
<p> </p>
<p><b>Comments: </b></p>
<p><textarea rows="2" name="S1" cols="100"></textarea><br>
<br>
</p>
<p>
<font size="2"><b>
<input type="submit" name="obbtn_yes"
value="Submit"></b></font></p>
</form>

</body>
 
P

pete0085

I think this is the code where my problem is but not sure how to fix it. I
have one text box called "date", the other is "date1".

I tried to make two separate scripts, but then it only validates the second
date.

function ValidateForm(){
var dt=document.frmSample.date
if (isDate(dt.value)==false){
dt.focus()
return false
}
return true
 
R

Ronx

Change the function to:

function ValidateForm(){
var dt=document.frmSample.date;
var dt1=document.frmSample.date1;
if (isDate(dt.value)==false) {
dt.focus();
return false;
}
if (isDate(dt1.value)==false){
dt1.focus();
return false;
}
return true;
}

--
Ron Symonds
Microsoft MVP (Expression)
http://www.rxs-enterprises.org/fp

Reply only to group - emails will be deleted unread.
 
P

pete0085

Thank you so much! That works great!

Ronx said:
Change the function to:

function ValidateForm(){
var dt=document.frmSample.date;
var dt1=document.frmSample.date1;
if (isDate(dt.value)==false) {
dt.focus();
return false;
}
if (isDate(dt1.value)==false){
dt1.focus();
return false;
}
return true;
}

--
Ron Symonds
Microsoft MVP (Expression)
http://www.rxs-enterprises.org/fp

Reply only to group - emails will be deleted unread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top