home *** CD-ROM | disk | FTP | other *** search
- LISTINGS:
- Listing 1.
- <HTML>
- <HEAD>
- <TITLE>Registration Validation Sample</TITLE>
- <SCRIPT LANGUAGE="JavaScript">
-
- //Validate that this string contains no digits
- function CheckAlpha(str, min, max)
- {
- if (str.length < min || str.length > max)
- return false
-
- for (var i = 0; i < str.length; i++) {
- var ch = str.substring(i, i + 1)
- if (ch >= "0" && ch <= "9") {
- return false
- }
- }
-
- return true
- }
-
-
- //Validate that this string contains only
- // digits
- function CheckNum(str, min, max)
- {
- if (str.length < min || str.length > max)
- return false
-
- for (var i = 0; i < str.length; i++) {
- var ch = str.substring(i, i + 1)
- if (ch < "0" || ch > "9") {
- alert(alertStr)
- return false
- }
- }
-
- return true
- }
-
-
- // Check all the fields of the form
- // for validity.
- function checkForm(form)
- {
- // first, check that the name has no numbers in it
- // that isn't a valid name
- if (!CheckAlpha(form.fldName.value, 1, 65)) {
- alert("You must enter a valid name")
- return false
- }
-
-
- // Next, check that there was at least one address
- // field and a city field entered.
- // Note that numbered cities are also invalid
- if (form.fldAddr1.value == "" && form.fldAddr2.value _
- == "") {
- alert("You must enter at least one address line")
- return false
- }
- if (!CheckAlpha(form.fldCity.value, 1, 65)) {
- alert("Invalid city name")
- return false
- }
-
- // Better check the state, too
- if (!CheckAlpha(form.fldState.value, 2, 2)) {
- alert("Invalid state code")
- return false
- }
-
-
- // Now validate the zip code
- if (!CheckNum(form.fldZip.value, 5, 9)) {
- alert("Invalid zipcode")
- return false
- }
-
- // Passed all the tests. The form may be submitted
- return true
-
- }
-
- </script
-
- // end hiding contents from old browsers -->
-
-
- </HEAD>
-
-
- <!-- Now, the body of the form -->
-
- <H1 align=center>Mike's JavaScript Form validator</H1>
-
- <FORM NAME="frmRegister" METHOD="POST" onSubmit="if _
- (!checkForm(frmRegister)) return false">
-
- <P>
- Name: <INPUT NAME="fldName" VALUE="" MAXLENGTH="65" _
- SIZE=65><BR>
- Address1: <INPUT NAME="fldAddr1" VALUE="" _
- MAXLENGTH="65" SIZE=65>
- <BR>
- Address2: <INPUT NAME="fldAddr2" VALUE="" _
- MAXLENGTH="65" SIZE=65>
- <BR>
- City: <INPUT NAME="fldCity" VALUE="" MAXLENGTH="35" _
- SIZE=35> State:
- <INPUT NAME="fldState" VALUE="" MAXLENGTH="2" SIZE=2>
- Zipcode<input name="fldZip" VALUE="" MAXLENGTH=9 _
- size=9><BR><BR>
- <INPUT TYPE=SUBMIT VALUE="Submit" NAME="btnSubmit">
- </FORM>
-
-
- </BODY>
- </HTML>
-
- LISTING NO. 2
- <HTML>
- <HEAD>
- <TITLE>JavaScript Clock</TITLE>
- <SCRIPT LANGUAGE="JavaScript">
-
- <!-- to hide script contents from old browsers
-
- // First set up the global variables needed for the
- // clock
- var idTimer = null;
- var fTimerOn = false;
- var fStatusDisplay = false;
-
-
- // We need an initialize/clear function for the timer
- // stop the clock if it is on, then clear the timer flag
- function stopclock (){
- if(fTimerOn)
- clearTimeout(idTimer);
- fTimerOn = false;
- }
-
-
- // Start up the timer clock.
- // lets try to only start it once!
- function startclock () {
- // Make sure the clock is stopped
- stopclock();
- showtime();
- }
-
-
- // Computer the current time
- // and stuff it into the timer field
- function showtime () {
-
- // get the date and time
- var now = new Date();
- var hours = now.getHours();
- var minutes = now.getMinutes();
- var seconds = now.getSeconds()
-
- // convert to a 12 hour clock
- // until the US understands a normal 24 hour clock
- var timeValue = "" + ((hours >12) ? hours -12 :hours)
- timeValue += ((minutes < 10) ? ":0" : ":") + minutes
- timeValue += ((seconds < 10) ? ":0" : ":") + seconds
- timeValue += (hours >= 12) ? " P.M." : " A.M."
- document.frmClock.fldClock.value = timeValue;
-
- // If we want to have the status bar update the time
- // set this flag to true
- if (fStatusDisplay) {
- window.status = timeValue;
- }
-
- // set the timer to fire once a second
- // and put the timer identifier into our global
- // call showtime every time the timer fires
- idTimer = setTimeout("showtime()",1000);
-
- // and set our timer on flag to true
- fTimerOn = true;
- }
-
- // end hiding Javascript from old browsers -->
- </script
-
- </HEAD>
-
- <BODY bgcolor="#ffffff" onLoad="startclock()">
-
- <H1 align=center>One Second timer Example</H1>
-
- <FORM NAME="frmClock" onSubmit="0">
-
- <P>
- The current time is: <INPUT NAME="fldClock" VALUE="" _
- MAXLENGTH="15" SIZE=15><BR>
- <P>
- See a clock on the status bar? <INPUT TYPE="checkbox" _
- value = false NAME="fldStatFlag" _
- onClick="fStatusDisplay = _
- this.form.fldStatFlag.checked">
- </FORM>
-
-
- </BODY>
- </HTML>
-
- 1 02/15/96 5:46 PM
-
-
-