On Tue, 24 Feb 2004 19:35:51 +0100, Martin Nadoll <> wrote:
> I am working on a form-validation script.
> There is a input-field where you input a float or integer numper (maximum
> price to output in database-query).
>
> But my Cold-Fusion Query generates a >error-message, if there is more
> than one dot in that value e.g. 15.60.50
>
> Is it possible to validate, that there is only one or no dot given as
> value for that field?
Yes. String validation is best performed with regular expressions. The
snipped below will check that the number in 'num' is either an integer, or
a (simple) floating point representation:
if( /^\d+(\.\d+)?$/.test( num )) {
// num is valid
} else {
// num contains letters, symbols, or more than one dot (.)
}
The expression permits the following:
- A string composed entirely of digits with a minimum of one digit (e.g.
512)
- A string that begins with at least one digit, followed by a single dot,
and ending with at least one digit (e.g. 4.2 or 16.333)
If users attempt to enter other valid number representations, such as 5e-2
(0.05), they will fail. Any value that contains a letter or symbol will
also fail.
Don't forget: you should always perform validation on the server.
Client-side validation is no substitute.
Hope that helps,
Mike
--
Michael Winter
d (replace ".invalid" with ".uk" to reply)