Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #8010

Re: Data Validation Functions

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: Data Validation Functions
Date 2011-11-04 22:42 +0200
Organization A noiseless patient Spider
Message-ID <j91inq$ud2$1@dont-email.me> (permalink)
References <keh8b71001v4v80k2ejo0557v6b31i8vqb@4ax.com>

Show all headers | View raw


11/4/2011 10:16 PM, Gene Wirchenko wrote:

>       I wish to validate data on a frontend.  One such validation is
> whether the input string is a number.  I could parseInt() or
> parseFloat(), but 1) I do not want to allow E-notation and 2) one form
> of number that I want to check for is a dollar amount.
[...]
>       If I do roll my own, one way to do it would be a regex.  Is this
> a good idea?

The advantage of using a regex is that you can then specify exactly the 
format you accept. One problem with the approach is that it is difficult 
to localize, if you ever need to consider different culture-specific 
formats and different currencies. But I'm afraid there aren't many 
options - studying the existing libraries and testing them is likely to 
be more work than setting up a regexp.

Note that typically you need to check that there are no extra characters 
- something that the standard routines don't check (they happily accept 
'42foobar'). For example, to check that the input is a number without 
E-part, you could match it against
/^[+-]?\d+(\.\d+)?$/
and if it passes, read it with parseFloat(). This would reject data like 
.42 which might or might not be desirable; you need to decide what to 
accept.

When reading a monetary amount, you should probably check for either no 
decimal part or a decimal part with exactly two digits (though this is 
in locale-specific too), since '$42.5' looks suspiciously like data error.

> I am not too concerned
> with speed (unless a given way is way slower than others).

Speed is irrelevant in matters like this. The computer has plenty of 
cycles to spend when reading user input; it was different in the old 
days of multiuser timesharing a few decades ago. :-)

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Data Validation Functions Gene Wirchenko <genew@ocis.net> - 2011-11-04 13:16 -0700
  Re: Data Validation Functions "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-11-04 22:42 +0200
    Re: Data Validation Functions Gene Wirchenko <genew@ocis.net> - 2011-11-04 14:04 -0700
  Re: Data Validation Functions Elegie <elegie@anonymous.invalid> - 2011-11-04 22:06 +0100
  Re: Data Validation Functions Denis McMahon <denismfmcmahon@gmail.com> - 2011-11-04 21:45 +0000
    Re: Data Validation Functions Gene Wirchenko <genew@ocis.net> - 2011-11-04 15:11 -0700
      Re: Data Validation Functions Elegie <elegie@anonymous.invalid> - 2011-11-04 23:40 +0100
        Re: Data Validation Functions Gene Wirchenko <genew@ocis.net> - 2011-11-04 16:11 -0700
          Re: Data Validation Functions Tim Streater <timstreater@greenbee.net> - 2011-11-04 23:26 +0000

csiph-web