520 likes | 944 Views
Form Validation. Validation. Validation of form data can be cumbersome using the basic techniques StringTokenizer If-else statements Most of the time you want to validate against a pattern rather than a specific value E.g. all digits or all letters or letters with one space in between
E N D
Validation • Validation of form data can be cumbersome using the basic techniques • StringTokenizer • If-else statements • Most of the time you want to validate against a pattern rather than a specific value • E.g. all digits or all letters or letters with one space in between • What pattern would define any web URL? Any email address?
Regular Expressions • One technique is to define your pattern using what is called a Regular Expression (RegEx) and process it using a RegEx engine • For example, the regex (\d+) means one or more digits • Java SE and Android have built-in RegEx functionality • java.util.regex
Regular Expressions • Regular expressions (regex's) are sets of symbols and syntactic elements used to matchpatterns of text.
Matches • Input string consumed from left to right • Match ranges: inclusive of the beginning index and exclusive of the end index • Example: Current REGEX is: foo Current INPUT is: foofoofoo I found the text "foo" starting at index 0 and ending at index 3. I found the text "foo" starting at index 3 and ending at index 6. I found the text "foo" starting at index 6 and ending at index 9.
Quantifier Types • There are three types of regex matching styles: • Greedy • Reluctant • Possessive
Greedy • Example: Current REGEX is: .*foo Current INPUT is: xfooxxxxxxfoo I found the text "xfooxxxxxxfoo" starting at index 0 and ending at index 13. Greedy: first, the quantified portion of the expression eats the whole input string and tries for a match. If it fails, the matcher backs off the input string by one character and tries again, until a match is found. • Greedy is the default style
Reluctant • Example: Current REGEX is: .*?foo Current INPUT is: xfooxxxxxxfoo I found the text "xfoo" starting at index 0 and ending at index 4. I found the text "xxxxxxfoo" starting at index 4 and ending at index 13. Reluctant: starts to match at the beginning of the input string. Then, iteratively eats another character until the whole input string is eaten. • Note the ? On the expression, this indicates the previous portion should he handled reluctantly
Possessive • Example Current REGEX is: .*+foo Current INPUT is: xfooxxxxxxfoo No match found. Possessive: try to match only once on the whole input stream. • NOTE the + sign
Groups • With parentheses, we can create groups to apply quantifiers to several characters: “(abc)+” • Also useful for parsing results (see last slide) • Groups are numbered by counting their opening parentheses from left to right • Example: groups in “((A)(B(C)))” 1. ((A)(B(C))) 2. (A) 3. (B(C)) 4. (C)
Boundary matchers Current REGEX is: \bdog\b Current INPUT is: The doggie plays in the yard. No match found. Current REGEX is: \bdog\b Current INPUT is: The dog plays in the yard. Match found.
String • The String class contains several regex based methods • split(String regex) – splits a String using the regEx as a delimiter • replaceAll(String regex, String replacement) – replaces all match patterns with replacement • matches(String regex) – returns if the string matches the regex • These methods are sufficient for most simple validation tasks
split() example • NOTE: notice we are splitting against a literal period. • Since the period is part of the regex language, it must be escaped -> \. • But since you need to escape the backslash in Java -> \\. String testString = "This.is.a.test"; String[] data = testString.split("\\."); System.out.println(Arrays.toString(data)); OUTPUT: [This, is, a, test]
replaceAll() example • NOTE: we need to reassign the output string of the method to the variable since String instances are immutable String testString = "This.is.a.test.test.test"; testString = testString.replaceAll("\\.test", ""); System.out.println(testString); OUTPUT: This.is.a
matches() example • Regex description: • Starts with “This” • Has one or more groups of one or more whitespaces followed by one or more word characters • Followed by a single whitespace • Followed by “test” String match = "This is a test"; System.out.println(match.matches("This(\\s+\\w+)+\\stest")); OUTPUT: true
Java RegEx API • Two important classes: • java.util.regex.Pattern -- a compiled representation of a regular expression • java.util.regex.Matcher -- an engine that performs match operations by interpreting a Pattern • Example Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();
Pattern • Patterns are compiled regular expressions. • Pattern instances are useful if you need to do a lot of work with the same regular expression • more efficient to compile it once and reuse it. • e.g. for parsing tasks
Pattern • The Pattern class and its companion, Matcher, are also a lot more powerful than the small amount of functionality exposed by String • Can be used to find repeating patterns within a string • A Pattern is created using the static Pattern.compile(String regEx)method
Matcher • Once a Pattern is compiled, a Matcher instance can be obtained using matcher(String inputString)method of Pattern • Some common Matcher methods: • matches() – used to determine if the inputString matches the pattern used to create this Matcher • find() – used to loop through all the pattern matches in the inputString • group(int index) – isolates the value held in a specific group of the match • 1-indexed, group 0 represents the whole match
Example • Regex description: • Starts with zero or more “a” characters • Followed by “b” Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches(); OUTPUT: true
Example import java.util.regex.*; public class RegEx{ public static void main( String args[] ){ String amounts = "$1.57 $316.15 $19.30 $0.30 $0.00 $41.10 $5.1 $.5"; Pattern strMatch = Pattern.compile( "\\$(\\d+)\\.(\\d\\d)" ); Matcher m = strMatch.matcher( amounts ); while ( m.find() ){ System.out.println( "$" + ( Integer.parseInt( m.group(1) ) + 5 ) + "." + m.group(2) ); } } } OUTPUT: $6.57 $321.15 $24.30 $5.30 $5.00 $46.10 • Regex description: • Starts with “$” • Followed by one or more digits • Followed by a period • Followed by two digits
Additional References • SUN regexps tutorial http://download.oracle.com/javase/tutorial/essential/regex/ • Java.util.regex API for Android file:///C:/android-sdk-windows/docs/reference/java/util/regex/package-summary.html
UI Validation in Android • EditText widgets have a means of limiting what types of characters using the inputType XML attribute • However, this is not enough to handle specific cases such as • does the cellphone number match a Philippine number (+63xxxxxxx) or a Globe/SMART/Sun number
UI Validation in Android • Regex Validation can be done in several places • Check the current text as you are typing • Requires a KeyListener • Can be a bit tricky • At a predefined point (e.g. when moving to the next screen) • Prior to triggering the next activity or closing the current activity extract all the strings and check against the patterns
Things to think about What are the expressions for the following? Email address Web URL Cellphone/Phone number License plate Money values (with commas and decimal) Strong passwords