| Server IP : 23.94.149.180 / Your IP : 216.73.216.147 Web Server : nginx/1.20.1 System : Linux deadrock 5.14.0-741.22.1.el9_9.x86_64 #1 SMP PREEMPT_DYNAMIC Thu Nov 12 15:18:14 UTC 2026 x86_64 User : nginx ( 991) PHP Version : 8.6.0 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /files/www/hacklab/peterson/ |
Upload File : |
<html>
<title> CIS25 Validate </title>
<head>
<script language="JavaScript">
<!-- begin JS hide
/*JavaScript Homework #3 for CIS25, Spring 2000, Thursday evening class
Using Netscape 4.7
by: Sandy Vilahu, Sharon Newman and Linda Peterson
Cut and Paste JavaScript URL: http://www.htmlgoodies.com/primers/jsp/hgjsp_29.html
Script contains the following events, properties and functions:
charAt() (Method)
focus (Method)
indexOf() (Method)
length (Property)
onBlur (Event)
onClick (Event)
validFirstName (Function)
validZip (Function)
Once we have created an HTML feedback form, it only makes sense that you would want
to insure that the user properly fills out all the form's fields. Using JavaScript
to validate your HTML form on the user's machine prior to returning the information
to your server for processing is the most efficient means of form validation.
The cut-and-paste JavaScript we are presenting shows form validation for the First
Name and the Zip code. To validate data a function is created defining the required
data to be returned. The user inputs information and once the submit key is clicked
JavaScript goes to work behind the scenes verifying the information is of the
appropriate form. It is important to note that this only works with Navigator 3.0
or higher and IE 3 and up. However, while documentation suggests Navigator 3.0 as
workable we found that it was buggy. To handle clients that are using older
browsers CGI is used on the server side.
We will describe the method focus, the event onBlur, and the Functions
validFirstName and validZip.
onBlur( ) - Used to remove focus from the selected object by invoking the
object's onblur( ) method. The inverse user action to focus is blur.
When a user tabs or clicks away from a text field the action of
leaving the field is called blur. onBlur( ) is an eventhandler.
OnBlur makes something happen when the user leaves a text field.
It is the recommended eventhandler for validation as it forces the
user to enter new data in a text box before moving out of the field,
it will not allow the user to just tab through leaving an empty field
or the old incorrect data.
onBlur is available to use in Navigator 2 and up and IE3 and up
focus( ) - Used to bring focus to a selected object by involking the object's
focus( ) method. A text field is said to have focus when the user
clicks or tabs into a field. The curser is flashing at that field
and the user can enter text into the field. Only one text field can
have focus at a time.
focus( ) is available to use in Navigator 2 and up and IE 3 and up
validFirstName - Function - In simple terms, this function, validFirstName, (fnm)
checks the first name field for any keyed characters. The function is
called by the onBlur event. As the focus moves away from the first
name text field, onBlur is called and onBlur calls the function. The
first step is to pass the argument 'fn.value' from the form to the
function (from the first name field to the 'validFirstName' function)
� a variable called fnlen (first name length) is assigned the value
of the length of the keyed text. Check out the string.length
property on page 682.
� If the first name is Linda or Sandy of Jamie(each of which has 5
characters), then the 'value' of the first name is 5. If the
length (value) of the first name is anything other than 0, then
the focus remains off of the first name field and the user is not
shown any error message for the first name field. Anything keyed
equals a name to this routine.
� If the value of the keyed text is equal to 0, then the user hasn't
typed a name (or anything) in the field. The program gives an
error message by displaying the alert: "First name is required"
and returns the focus to the name field.
Document.dataentry.fn.focus().
The validation function assigns a value to the keyed text using the
length property and then just compares the length value to 0. If the
value equals 0, then the program returns the user to the field to try
again. If the value equals anything other than 0, the text entered is
'validated' and the program proceeds. The validation standard for the
first name is rather low.
validZip - Function - The validZip function is a similar process, but is more
involved because it is checking for more than one value.
� the variable 'len' is set to equal the length of the zip code.
(There's that length property again.)
� the variable 'digits' is given the value of all the digit values
0 through 9.
� The 'If' statement then checks for two length properties - not
equal to 5 and not equal to 10. The double ampersand && is where
the Javascript is checking the two properties.
� If the two properties checked are true (the keyed text is not equal
to 5 and not equal to 10) then the focus is returned to the Zip
field and the user is advised that the first five digits must be
numeric.
� The balance of the function includes additional instructions for
more specific validation of the keyed text to determine if the
characters keyed are actually numbers. There are two other methods
in this segment of the program, charAt() and indexOf() which are
part of the zip validation function.
*/
function validFirstName(fnm) // The first function is validFirstName(fnm).
{
fnlen=fnm.length // The variable fnlen is assigned the value of the
// length of the name
if (fnlen == 0) // If the firstname variable length equals 0, then
{alert("First name is required") // Display alert to user that the name is required and
document.dataentry.fn.focus()} // return the focus to the dataentry first name field
}
function validZip(zip) // The second function in this program.
{
len=zip.length // The variable len is assigned the value of the length zip
digits="0123456789" // The variable digits is given the value of all the digits 0-9
if(len != 5 && len != 10) // If the length of the zip doesn't equal 5 and doesn't
// equal 10,
{alert("Zip is not the correct length") //Then display the alert and
document.dataentry.zip.focus() //Return focus to the zip field so the user can rekey
}
for(i=0; i<5; i++) // Looks at the first five digits, one at a time.
{if (digits.indexOf(zip.charAt(i))<0)
{alert("First five digits must be numeric")
document.dataentry.zip.focus()
break}
}
}
// end JS hide -->
</script>
</head>
<body bgcolor="#B0C4DE">
<form name="dataentry">
<h2>Understanding Form Field Validation</h2>
Enter First Name:<br>
<input type="text" NAME="fn"
onBlur="validFirstName(fn.value)"> <!-- when user trys to leave text box
first name function is called -->
<script language="JavaScript">
document.dataentry.fn.focus()
</script>
<p>
Enter Zip Code (99999-9999):<br>
<input type="text" NAME="zip" SIZE=10 > <!-- when user clicks submit button
zip code validate function is called-->
<P>
<input type="button" VALUE="Submit"
onClick="validZip(zip.value)">
</body>
</html>