home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The JavaScripts implement a number of tests. In addition to making sure that the expiration date entered has not passed, the credit card number entered is checked to see whether it is well formed. The JavaScript checks employ both the prefix and length text, as well as the Luhn Algorithm check digit test. Figure 21.6 shows the results of a well-formed credit card number of the card type selected.

As shown in Figure 21.7, the JavaScript tests are even smart enough to determine when a well-formed card number of a type other than the one selected has been entered. If the number is not a well-formed card number for any known type of card, an Alert box with that information appears (see Figure 21.8).


FIGURE 21.6  Both the check digit and prefix and length test need to pass to correctly identify the card number as well formed for the given type.


FIGURE 21.7  Using the prefix and length information, it is possible to automatically determine the type of a misidentified card.


FIGURE 21.8  If the check digit test is failed, the number cannot be a valid credit card number of any known type.

Netscape’s Sample Form Validation Scripts

A veritable treasure trove of freely available JavaScripts for form validation is available from the Netscape Web site, available on the accompanying CD-ROM and also at the following URL:
http://developer.netscape.com/library/examples/javascript/formval/overview.html

The JavaScript source file that you can download from that site is called FormChek.js. In addition, a number of sample HTML files show the functions in operation. The JavaScript functions defined there come under a number of general categories, as described in the header information in the FormChek.js file (summarized in the following sections).

Data Validation Functions

The purpose of these JavaScripts is to check strings and characters, normally those entered into HTML Form text fields. The functions can also be more generally applied to determine whether the strings are a given type of data. The basic functions are the following:

  isWhitespace(s)—Check whether string s is empty or whitespace.
  isLetter(c)—Check whether character c is an English letter.
  isDigit(c)—Check whether character c is a digit.
  isLetterOrDigit(c)—Check whether character c is a letter or digit.
  isInteger(s,ok_if_empty?)—True if all characters in string s are numbers.
  isSignedInteger(s,ok_if_empty?)—True if all characters in string s are numbers; leading + or - allowed.
  isPositiveInteger(s,ok_if_empty?)—True if string s is an integer > 0.
  isNonnegativeInteger(s,ok_if_empty?)—True if string s is an integer >= 0.
  isNegativeInteger(s,ok_if_empty?)—True if s is an integer < 0.
  isNonpositiveInteger(s,ok_if_empty?)—True if s is an integer <= 0.
  isFloat(s,ok_if_empty?)—True if string s is an unsigned floating point (real) number. (Integers also okay.)
  isSignedFloat(s,ok_if_empty?)—True if string s is a floating point or integer number; leading + or - allowed.
  isAlphabetic(s,ok_if_empty?)—True if string s is English letters.
  isAlphanumeric(s,ok_if_empty?)—True if string s is English letters and numbers only.

The preceding functions are used to build up more specialized data validation functions, as shown in the list below:

  isSSN(s,ok_if_empty?)—True if string s is a valid U.S. Social Security number.
  isUSPhoneNumber(s,ok_if_empty?)—True if string s is a valid U.S. phone number.
  isInternationalPhoneNumber(s,ok_if_empty?)—True if string s is a valid international phone number.
  isZIPCode(s,ok_if_empty?)—True if string s is a valid U.S. zip code.
  isStateCode(s,ok_if_empty?)—True if string s is a valid U.S. postal code.
  isEmail(s,ok_if_empty?)—True if string s is a valid email address.
  isYear(s,ok_if_empty?)—True if string s is a valid year number.
  isIntegerInRange(s,a,b,ok_if_empty?)—True if string s is an integer between a and b, inclusive.
  isMonth(s,ok_if_empty?)—True if string s is a valid month between 1 and 12.
  isDay(s,ok_if_empty?)—True if string s is a valid day between 1 and 31.
  daysInFebruary(year)—Returns number of days in February of that year.
  isDate(year,month,day)—True if string arguments form a valid date.

Data Formatting Functions

As you saw with the JavaScript example presented in the beginning of this chapter, it is often necessary or desirable to reformat information entered into the HTML Form text fields, either before processing it or afterward for redisplay. By doing this, it is possible to strip dollar signs or commas from entered currency amounts, format phone number or credit card numbers to display in a uniform way, or to perform any other function meant to manipulate data for processing or display. The following list identifies the JavaScripts available from the Netscape Web site for doing these functions:

  stripCharsInBag(s,bag)—Removes all characters in string bag from string s.
  stripCharsNotInBag(s,bag)—Removes all characters not in string bag from string s.
  stripWhitespace(s)—Removes all whitespace characters from s.
  stripInitialWhitespace(s)—Removes leading whitespace characters from s.
  reformat(target,[string,integer,...])—Function for inserting formatting characters or delimiters into target string.
  reformatZIPCode(ZIPString)—If 9 digits, inserts separator hyphen.
  reformatSSN(SSN)—Reformats as 123-45-6789.
  reformatUSPhone(USPhone)—Reformats as (123) 456-789.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.