php file is a very basic example of how you might do regexp-based validation
in PHP on the server. Each field is given a regexp to match against what the user
submits and an error message to display if it doesn??™t match. For example, the ???number of
heads??? field must contain digits, so its regexp is ^\d+$. (Note the +, meaning ???one or more of
these,??? which makes this field compulsory.) If completing the field were optional (so it could
be left blank), the regexp would have been ^\d*$ because * means ???zero or more of these.???
Note also that some of these regexps are fairly simplistic. Regular expressions are good
but imperfect tools. For example, the e-mail regexp??”^.+@.+\..+$??”allows invalid nastinesses
such as stuart@somewhere@somewhere@somewhere.com. You often cannot completely rely on
a regexp to give you validity. http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html has
CHAPTER 7 n FORM VALIDATION AND JAVASCRIPT 149
a ???proper??? regexp for matching e-mail addresses, which is a mighty 6251 characters long.
In practice, what you??™re doing with validation here is stripping out things that are obviously
wrong, not trying to catch every single invalid case.
The Client Side
On to JavaScript. JavaScript supports regexps natively; a string has a search() method that
takes a regexp as parameter and returns the first character where the regexp is found in the
string, or -1 if it??™s not found at all.
Pages:
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222