home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Software Sampler / Visual_Basic_Software_Sampler_Visual_Basic_Programmers_Journal_June_1996.iso / issues / 04apr96 / code / fp51.txt < prev    next >
Text File  |  1996-04-24  |  5KB  |  217 lines

  1. LISTINGS:
  2. Listing 1.
  3. <HTML>
  4. <HEAD>
  5. <TITLE>Registration Validation Sample</TITLE>
  6. <SCRIPT LANGUAGE="JavaScript">
  7.  
  8. //Validate that this string contains no digits
  9. function CheckAlpha(str, min, max)
  10. {
  11.     if (str.length < min || str.length > max)
  12.         return false
  13.  
  14.     for (var i = 0; i < str.length; i++) {
  15.         var ch = str.substring(i, i + 1)
  16.         if (ch >= "0" && ch <= "9") {
  17.             return false
  18.         }
  19.     }
  20.  
  21.     return true
  22. }
  23.  
  24.  
  25. //Validate that this string contains only
  26. // digits
  27. function CheckNum(str, min, max)
  28. {
  29.     if (str.length < min || str.length > max)
  30.          return false
  31.  
  32.     for (var i = 0; i < str.length; i++) {
  33.         var ch = str.substring(i, i + 1)
  34.         if (ch < "0" || ch > "9") {
  35.                     alert(alertStr)
  36.             return false
  37.         }
  38.     }
  39.  
  40.     return true
  41. }
  42.  
  43.  
  44. // Check all the fields of the form
  45. // for validity.
  46. function checkForm(form)
  47. {
  48.     // first, check that the name has no numbers in it
  49.     // that isn't a valid name
  50.     if (!CheckAlpha(form.fldName.value, 1, 65)) {
  51.         alert("You must enter a valid name")
  52.         return false
  53.     }
  54.  
  55.  
  56.     // Next, check that there was at least one address 
  57.     // field and a city field entered.
  58.     // Note that numbered cities are also invalid
  59.     if (form.fldAddr1.value == "" && form.fldAddr2.value _
  60.         == "") {
  61.         alert("You must enter at least one address line")
  62.         return false
  63.     }
  64.     if (!CheckAlpha(form.fldCity.value, 1, 65)) {
  65.         alert("Invalid city name")
  66.         return false
  67.     }
  68.  
  69.     // Better check the state, too
  70.     if (!CheckAlpha(form.fldState.value, 2, 2)) {
  71.         alert("Invalid state code")
  72.         return false
  73.     }
  74.  
  75.  
  76.     // Now validate the zip code
  77.     if (!CheckNum(form.fldZip.value, 5, 9)) {
  78.         alert("Invalid zipcode")
  79.         return false
  80.     }
  81.  
  82.     // Passed all the tests. The form may be submitted
  83.     return true
  84.  
  85. }
  86.  
  87. </script
  88.  
  89. // end hiding contents from old browsers  -->
  90.  
  91.  
  92. </HEAD>
  93.  
  94.  
  95. <!-- Now, the body of the form -->
  96.  
  97. <H1 align=center>Mike's JavaScript Form validator</H1>
  98.  
  99. <FORM NAME="frmRegister" METHOD="POST" onSubmit="if _
  100.     (!checkForm(frmRegister)) return false">
  101.  
  102. <P>
  103.     Name: <INPUT NAME="fldName" VALUE="" MAXLENGTH="65" _
  104.         SIZE=65><BR>
  105. Address1: <INPUT NAME="fldAddr1" VALUE="" _
  106.     MAXLENGTH="65" SIZE=65>
  107. <BR>
  108. Address2: <INPUT NAME="fldAddr2" VALUE="" _
  109.     MAXLENGTH="65" SIZE=65>
  110. <BR>
  111.     City: <INPUT NAME="fldCity" VALUE="" MAXLENGTH="35" _
  112.         SIZE=35> State:
  113. <INPUT NAME="fldState" VALUE="" MAXLENGTH="2" SIZE=2>
  114. Zipcode<input name="fldZip" VALUE="" MAXLENGTH=9 _
  115.     size=9><BR><BR>
  116. <INPUT TYPE=SUBMIT VALUE="Submit" NAME="btnSubmit">
  117. </FORM>
  118.  
  119.  
  120. </BODY>
  121. </HTML>
  122.  
  123. LISTING NO. 2
  124. <HTML>
  125. <HEAD>
  126. <TITLE>JavaScript Clock</TITLE>
  127. <SCRIPT LANGUAGE="JavaScript">
  128.  
  129. <!--  to hide script contents from old browsers
  130.  
  131. // First set up the global variables needed for the
  132. // clock
  133. var idTimer = null;
  134. var fTimerOn = false;
  135. var fStatusDisplay = false;
  136.  
  137.  
  138. // We need an initialize/clear function for the timer
  139. // stop the clock if it is on, then clear the timer flag
  140. function stopclock (){
  141.     if(fTimerOn)
  142.         clearTimeout(idTimer);
  143.     fTimerOn = false;
  144. }
  145.  
  146.  
  147. // Start up the timer clock.
  148. // lets try to only start it once!
  149. function startclock () {
  150.     // Make sure the clock is stopped
  151.     stopclock();
  152.     showtime();
  153. }
  154.  
  155.  
  156. // Computer the current time
  157. // and stuff it into the timer field
  158. function showtime () {
  159.  
  160.     // get the date and time
  161.     var now = new Date();
  162.     var hours = now.getHours();
  163.     var minutes = now.getMinutes();
  164.     var seconds = now.getSeconds()
  165.  
  166.     // convert to a 12 hour clock
  167.     // until the US understands a normal 24 hour clock
  168.     var timeValue = "" + ((hours >12) ? hours -12 :hours)
  169.     timeValue += ((minutes < 10) ? ":0" : ":") + minutes
  170.     timeValue += ((seconds < 10) ? ":0" : ":") + seconds
  171.     timeValue += (hours >= 12) ? " P.M." : " A.M."
  172.     document.frmClock.fldClock.value = timeValue;
  173.  
  174.     // If we want to have the status bar update the time
  175.     // set this flag to true
  176.     if (fStatusDisplay) {
  177.         window.status = timeValue;
  178.     }
  179.  
  180.     // set the timer to fire once a second
  181.     // and put the timer identifier into our global
  182.     // call showtime every time the timer fires
  183.     idTimer = setTimeout("showtime()",1000);
  184.  
  185.     // and set our timer on flag to true
  186.     fTimerOn = true;
  187. }
  188.  
  189. // end hiding Javascript from old browsers -->
  190. </script
  191.  
  192. </HEAD>
  193.  
  194. <BODY bgcolor="#ffffff" onLoad="startclock()">
  195.  
  196. <H1 align=center>One Second timer Example</H1>
  197.  
  198. <FORM NAME="frmClock" onSubmit="0">
  199.  
  200. <P>
  201. The current time is: <INPUT NAME="fldClock" VALUE="" _
  202.     MAXLENGTH="15" SIZE=15><BR>
  203. <P>
  204. See a clock on the status bar? <INPUT TYPE="checkbox" _
  205.     value = false NAME="fldStatFlag" _
  206.     onClick="fStatusDisplay = _
  207.     this.form.fldStatFlag.checked">
  208. </FORM>
  209.  
  210.  
  211. </BODY>
  212. </HTML>
  213.  
  214. 1    02/15/96    5:46 PM
  215.  
  216.  
  217.