<SCRIPT LANGUAGE="javascript">
var conn = new ActiveXObject("ADODB.Connection");
var connectionstring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:/sabe/Sabe.mdb; Persist Security Info=False";
conn.Open(connectionstring);
var rs = new ActiveXObject("ADODB.Recordset");
rs.Open("SELECT [itemname] FROM item", conn);
while(!rs.eof)
{
alert(rs(0));
rs.movenext;
}
rs.close;
conn.close;
</SCRIPT>
Thursday, February 17, 2005
Thursday, February 10, 2005
Date Validation with JavaScript
This is a small bit of html to show you how you can use JavaScript to validate dates. This is also leap year compliant.
<html>
<head>
<script type="text/javascript">
function isValidDate(dateStr)
{
if (dateStr == "MM/DD/YYYY")
{
return false;
}
// dateStr must be of format month day year with either slashes
// or dashes separating the parts. Some minor changes would have
// to be made to use day month year or another format.
// This function returns True if the date is valid.
var slash1 = dateStr.indexOf("/");
if (slash1 == -1)
{
slash1 = dateStr.indexOf("-");
}
// if no slashes or dashes, invalid date
if (slash1 == -1)
{
return false;
}
var dateMonth = dateStr.substring(0, slash1)
var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
var slash2 = dateMonthAndYear.indexOf("/");
if (slash2 == -1)
{
slash2 = dateMonthAndYear.indexOf("-");
}
// if not a second slash or dash, invalid date
if (slash2 == -1)
{
return false;
}
var dateDay = dateMonthAndYear.substring(0, slash2);
var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
if ( (dateMonth == "") (dateDay == "") (dateYear == "") )
{
return false;
}
// if any non-digits in the month, invalid date
for (var x=0; x < dateMonth.length; x++)
{
var digit = dateMonth.substring(x, x+1);
if ((digit < "0") (digit > "9"))
{
return false;
}
}
// convert the text month to a number
var numMonth = 0;
for (var x=0; x < dateMonth.length; x++)
{
digit = dateMonth.substring(x, x+1);
numMonth *= 10;
numMonth += parseInt(digit);
}
if ((numMonth <= 0) (numMonth > 12))
{
return false;
}
// if any non-digits in the day, invalid date
for (var x=0; x < dateDay.length; x++)
{
digit = dateDay.substring(x, x+1);
if ((digit < "0") (digit > "9"))
{
return false;
}
}
// convert the text day to a number
var numDay = 0;
for (var x=0; x < dateDay.length; x++)
{
digit = dateDay.substring(x, x+1);
numDay *= 10;
numDay += parseInt(digit);
}
if ((numDay <= 0) (numDay > 31))
{
return false;
}
// February can't be greater than 29 (leap year calculation comes later)
if ((numMonth == 2) && (numDay > 29)) { return false; }
// check for months with only 30 days
if ((numMonth == 4) (numMonth == 6) (numMonth == 9) (numMonth == 11))
{
if (numDay > 30)
{
return false;
}
}
// if any non-digits in the year, invalid date
for (var x=0; x < dateYear.length; x++)
{
digit = dateYear.substring(x, x+1);
if ((digit < "0") (digit > "9"))
{
return false;
}
}
// convert the text year to a number
var numYear = 0;
for (var x=0; x < dateYear.length; x++)
{
digit = dateYear.substring(x, x+1);
numYear *= 10;
numYear += parseInt(digit);
}
// Year must be a 2-digit year or a 4-digit year
if ( dateYear.length != 4 )
{
return false;
}
// check for leap year if the month and day is Feb 29
if ((numMonth == 2) && (numDay == 29))
{
var div4 = numYear % 4;
var div100 = numYear % 100;
var div400 = numYear % 400;
// if not divisible by 4, then not a leap year so Feb 29 is invalid
if (div4 != 0)
{
return false;
}
// at this point, year is divisible by 4. So if year is divisible by
// 100 and not 400, then its not a leap year so Feb 29 is invalid
if ((div100 == 0) && (div400 != 0))
{
return false;
}
}
// date is valid
return true;
}
</script>
</head>
<body>
<form name="aForm" id="aForm">
<input type="text" name="aDate" id="aDate" value="MM/DD/YYYY"/>
<input type="button" value="Check Date" onclick="alert(isValidDate(document.aForm.aDate.value));"/>
</form>
</body>
</html>
<html>
<head>
<script type="text/javascript">
function isValidDate(dateStr)
{
if (dateStr == "MM/DD/YYYY")
{
return false;
}
// dateStr must be of format month day year with either slashes
// or dashes separating the parts. Some minor changes would have
// to be made to use day month year or another format.
// This function returns True if the date is valid.
var slash1 = dateStr.indexOf("/");
if (slash1 == -1)
{
slash1 = dateStr.indexOf("-");
}
// if no slashes or dashes, invalid date
if (slash1 == -1)
{
return false;
}
var dateMonth = dateStr.substring(0, slash1)
var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
var slash2 = dateMonthAndYear.indexOf("/");
if (slash2 == -1)
{
slash2 = dateMonthAndYear.indexOf("-");
}
// if not a second slash or dash, invalid date
if (slash2 == -1)
{
return false;
}
var dateDay = dateMonthAndYear.substring(0, slash2);
var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
if ( (dateMonth == "") (dateDay == "") (dateYear == "") )
{
return false;
}
// if any non-digits in the month, invalid date
for (var x=0; x < dateMonth.length; x++)
{
var digit = dateMonth.substring(x, x+1);
if ((digit < "0") (digit > "9"))
{
return false;
}
}
// convert the text month to a number
var numMonth = 0;
for (var x=0; x < dateMonth.length; x++)
{
digit = dateMonth.substring(x, x+1);
numMonth *= 10;
numMonth += parseInt(digit);
}
if ((numMonth <= 0) (numMonth > 12))
{
return false;
}
// if any non-digits in the day, invalid date
for (var x=0; x < dateDay.length; x++)
{
digit = dateDay.substring(x, x+1);
if ((digit < "0") (digit > "9"))
{
return false;
}
}
// convert the text day to a number
var numDay = 0;
for (var x=0; x < dateDay.length; x++)
{
digit = dateDay.substring(x, x+1);
numDay *= 10;
numDay += parseInt(digit);
}
if ((numDay <= 0) (numDay > 31))
{
return false;
}
// February can't be greater than 29 (leap year calculation comes later)
if ((numMonth == 2) && (numDay > 29)) { return false; }
// check for months with only 30 days
if ((numMonth == 4) (numMonth == 6) (numMonth == 9) (numMonth == 11))
{
if (numDay > 30)
{
return false;
}
}
// if any non-digits in the year, invalid date
for (var x=0; x < dateYear.length; x++)
{
digit = dateYear.substring(x, x+1);
if ((digit < "0") (digit > "9"))
{
return false;
}
}
// convert the text year to a number
var numYear = 0;
for (var x=0; x < dateYear.length; x++)
{
digit = dateYear.substring(x, x+1);
numYear *= 10;
numYear += parseInt(digit);
}
// Year must be a 2-digit year or a 4-digit year
if ( dateYear.length != 4 )
{
return false;
}
// check for leap year if the month and day is Feb 29
if ((numMonth == 2) && (numDay == 29))
{
var div4 = numYear % 4;
var div100 = numYear % 100;
var div400 = numYear % 400;
// if not divisible by 4, then not a leap year so Feb 29 is invalid
if (div4 != 0)
{
return false;
}
// at this point, year is divisible by 4. So if year is divisible by
// 100 and not 400, then its not a leap year so Feb 29 is invalid
if ((div100 == 0) && (div400 != 0))
{
return false;
}
}
// date is valid
return true;
}
</script>
</head>
<body>
<form name="aForm" id="aForm">
<input type="text" name="aDate" id="aDate" value="MM/DD/YYYY"/>
<input type="button" value="Check Date" onclick="alert(isValidDate(document.aForm.aDate.value));"/>
</form>
</body>
</html>
Friday, February 04, 2005
Wednesday, October 13, 2004
Can you use JavaScript to read and write to a file?
Now I was searching high and low for the answer to this question. The big answer that I got was, JavaScript cannot read or write to files. However, it is possible to do this using ActiveX embedded into your JavaScript. The following is the code to do this.
//-------------------------------------------------------
function WriteToFile()
{
var filename = "data.txt";
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(filename))
{
var a, ForAppending, file;
ForAppending = 8;
file = fso.OpenTextFile(filename, ForAppending, false);
file.WriteLine(name);
file.WriteLine(password);
}
else
{
var file = fso.CreateTextFile(filename, true);
file.WriteLine(name);
file.WriteLine(password);
}
file.Close();
}
//-------------------------------------------------------
function ReadFromFile()
{
var fso, a, ForReading;
ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.OpenTextFile(filename, ForReading, false);
var name = file.readline();
var password = file.readline();
file.Close();
}
//-------------------------------------------------------
In IE you can use the FileSystemObject to read and write to/from files.
You can get to a reference on how to do this all here.
//-------------------------------------------------------
function WriteToFile()
{
var filename = "data.txt";
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (fso.FileExists(filename))
{
var a, ForAppending, file;
ForAppending = 8;
file = fso.OpenTextFile(filename, ForAppending, false);
file.WriteLine(name);
file.WriteLine(password);
}
else
{
var file = fso.CreateTextFile(filename, true);
file.WriteLine(name);
file.WriteLine(password);
}
file.Close();
}
//-------------------------------------------------------
function ReadFromFile()
{
var fso, a, ForReading;
ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
file = fso.OpenTextFile(filename, ForReading, false);
var name = file.readline();
var password = file.readline();
file.Close();
}
//-------------------------------------------------------
In IE you can use the FileSystemObject to read and write to/from files.
You can get to a reference on how to do this all here.
Sunday, October 03, 2004
My approach to drop down menus using JavaScript
Drop down menu
<div>
<iframe></iframe>
<div></div>
</div>
Hide outer div by setting the Style.Visibility to "hidden".
Show it when you want to by setting Style.Visibility to "visible".
Be sure to set the z-index of the internal div higher than the z-index of the iframe; if you do not, then the iframe will show over the div instead of under. Div tags are windowed elements while iframes are considered windowless. That means that unless you set the z-index higher, the iframe will always show over the div.
For an example, please contact me until I have a link up for an example.
<div>
<iframe></iframe>
<div></div>
</div>
Hide outer div by setting the Style.Visibility to "hidden".
Show it when you want to by setting Style.Visibility to "visible".
Be sure to set the z-index of the internal div higher than the z-index of the iframe; if you do not, then the iframe will show over the div instead of under. Div tags are windowed elements while iframes are considered windowless. That means that unless you set the z-index higher, the iframe will always show over the div.
For an example, please contact me until I have a link up for an example.
Subscribe to:
Posts (Atom)
