Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 91 92 93 94 95 96 < 97 > 98 99 100 101 102 103 .. 166 >> Next

001 package org.javapitfalls.item30;
002
003 import java.util.*;
004 import java.text.*;
005 import java.util.Date;
006 import java.util.regex.*;
007
008 public class validateBean {
009 private String title;
010 private String marriedFlag;
Listing 30.1 validateBean.java
Form Validation Using Regular Expressions 263
011 private String hobbies[];
012 private String colors[];
013 private String ageGroup;
014 private String telephoneNumber;
015 private String birthDate;
016 private String ssn;
017 private String email;
018 private String comments;
019 private SimpleDateFormat dateFormat;
020 private String DATE_FORMAT_PATTERN;
021
022 private Hashtable errors;
023
024 public boolean validate() {
025 boolean errorsFound=false;
026 if (title.equals("")) {
027 errors.put("title","Please enter a valid title");
028 errorsFound=true;
029 }
030
031 if (telephoneNumber.equals("")) {
032 #") errors.put("telephoneNumber","Please enter a valid telephone 2
# ) 033 errorsFound=true;
034 }
035 else {
036 if (!(telephoneNumber.matches("\\+?([0-9]+-)+([0-9]+- 2
) + [0-9]+"))) {
037 errors.put("telephoneNumber","Please enter a valid 2
telephone format ### - ### - ####");
038 errorsFound=true;
039 }
040 }
041
Listing 30.1 (continued)
Social security numbers are typically nine-digit strings separated by dash delimiters, so our regular expression needs to capture the digit value and size constraints in a similar fashion as the telephone number pattern.
042 if (ssn.equals("")) {
043 errors.put("ssn","Please enter a valid Social Security #");
044 errorsFound=true;
045 }
046 else {
047 if (!(ssn.matches("(\\d{3}\\-?)+(\\d{2}\\-?)+\\d{4}+"))) {
Listing 30.1 (continued)
264 Item 30
048 errors.put("ssn","Please enter a valid SSN: ### - ## - ####");
049 errorsFound=true;
050 }
051 }
052
Listing 30.1 (continued)
Date of birth formats come in many different varieties, but our application will validate user input on entries that adhere to the "YYYY-MM-DD" format. The commented text shown in lines 63 to 77 was implemented previously for DOB validation. Again, the ability to do more with less is evident in this code segment.
053 if (birthDate.equals("")) {
054 errors.put("birthDate","Please enter a valid date");
055 errorsFound=true;
056 }
057 else {
058
059 if (!(birthDate.matches("(\\d{4}\\-?)+(\\d{2}\\- ?)+\\d{2}+"))) {
060 errors.put("birthDate","Please enter a valid date format: 2
(yyyy-mm-dd)");
061 errorsFound=true;
062 }
063 /*
064 Date date=null;
065 try
066 {
067 dateFormat.applyPattern(DATE_FORMAT_PATTERN)
068 // dateFormat.setLenient(false);
069 date = dateFormat.parse(birthDate);
070 }
071 catch(ParseException parseexception) { }
072 if (date==null) {
073 errors.put("birthDate","Please enter a valid date format: 2
(yyyy-mm-dd)");
074 birthDate="";
075 errorsFound=true;
076 }
077 */
078 }
Listing 30.1 (continued)
Email account validation is achieved with the pattern described on line 85. The pattern text checks for alphanumeric text, with underlines and periods, that is separated by the "at sign"(@) and terminated with a 3-byte extension.
Form Validation Using Regular Expressions 265
080 : if (email.equals("")) {
081: errors.put("email", "Please enter a valid email address");
082 : errorsFound=true;
083 : }
084 : else {
085: if (!(email.matches(" -A-Za-z0-9_. ] +@ [-A-Za-z0-9_. ] +\\ . [-A-Za-2
z]{2,}' ))) {
086: errors.put("email", Please enter a valid email address: ex. 2
name@company.org");
087: errorsFound=true;
088 : }
089: }
Listing 30.1 (continued)
A pet peeve of mine is being forced to fill out an inordinate amount of information about myself in order to gain access to something I'm interested in. This annoyance typically leads to puerile inputs, which become more vulgar the longer I have to keep typing. I know that I'm not the only one doing this because most sites have some kind of dirty word checking to ensure that improper language is not propagated to their database.
The code shown between lines 94 and 96 checks user input to ensure that inappropriate language is captured and replaced with more mild, less offensive text. The regular expression (darn | shoot ....) is run against the comment text input by the user, and if a match is found, replaced by the replaceAll(text) method of the Matcher class.
090 if (comments.equals("")) {
091 errors.put("comments","Please enter a comment");
092 errorsFound=true;
093 } else {
094 Pattern pattern = 2
Pattern.compile("(darn|shoot|damn|jerk|stupid|dummy)");
095 Matcher match = pattern.matcher(comments);
096 if (match.find()) { comments = match.replaceAll("#%&@") ; }
097 }
098 return errorsFound;
099 }
Listing 30.1 (continued)
The following code illustrates how to use parenthesized subexpressions to replace text and return a new string value. In the example below, my intention is to cut off all of the digits after the decimal point with the exception of the first two. This can be accomplished by matching the first two digits after the decimal point with the "\\.\\d\\d" pattern. If the code segment below is run, then testAnswer will print out 111.63.
Previous << 1 .. 91 92 93 94 95 96 < 97 > 98 99 100 101 102 103 .. 166 >> Next