HTML and CSS Reference
In-Depth Information
executed if the
if statement is True
executed if the
if statement is False
executed if first
if statement is True
Figure 10-17
Using Built-In Functions to Validate Data
When validating data, the JavaScript code may have to evaluate several criteria —
for example, to ensure that a text field is not blank or that it contains numeric
data (not text or characters). HTML forms accept data entered into a text field as text
character data, which means that the values must be converted to a number before they
can be tested or validated. Table 10-9 describes the two built-in functions, parseInt()
and parseFloat(), used to convert values and one function, isNaN(), used to test if the
converted value is a number.
Number Base
Radix is the number base
to which the integer value
should be converted.
A number base represents
the number of digits and
the placement of digits.
We use base 10 for our
decimal system. Base 2 is
binary, base 8 is octal, and
base 16 is hexadecimal.
parseFloat() and
parseInt() Built-in
The parseFloat() function
is a built-in function that
parses a string argument
and converts the value
into a decimal floating-
point number. If the
first character cannot be
converted to a number, the
result is NaN, which means
“not a number.” The
parseInt() built-in function
converts a value to an
integer. If a value of 12Wre
is entered, parseInt() will
convert to 12.
Table 10-9 Built-In Functions: parseInt(), parseFloat(), isNaN()
General form:
variable = parseInt(value, base)
converts to an integer. Value is any string, which can be a variable or literal; base is the number
base to which you want the string converted. A base of 2 means binary base number, an 8
means octal, and a 10 means decimal. The function returns an integer value, stripping the value
after the decimal point.
General form:
variable = parseFloat(value)
converts to a floating point number. Value is any string, which can be a variable or literal,
representing a floating-point number. A floating-point number is one with a fractional or
decimal value (including percentages). The function returns the value as a floating-point number.
General form:
isNaN means is Not a Number. Value is any value, which can be a variable or literal. The function
returns a Boolean condition of true or false.
Figure 10-18 on the next page shows how the values of the form are passed to the
validSalesAmt() user-defined function.
Search WWH ::

Custom Search