errors);
147 | }
148 | }
149 |
--------------------------------------------------------------------------------
/library/src/main/java/com/alexfu/formvalidator/rules/EmailRule.java:
--------------------------------------------------------------------------------
1 | package com.alexfu.formvalidator.rules;
2 |
3 |
4 | import com.alexfu.formvalidator.utils.Patterns;
5 |
6 | public class EmailRule extends RegexRule {
7 | public EmailRule(String errorMessage) {
8 | super(Patterns.EMAIL_ADDRESS, errorMessage);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/library/src/main/java/com/alexfu/formvalidator/rules/MinLengthRule.java:
--------------------------------------------------------------------------------
1 | package com.alexfu.formvalidator.rules;
2 |
3 | public class MinLengthRule implements ValidationRule {
4 | public final int length;
5 | private final String errorMessage;
6 |
7 |
8 | public MinLengthRule(int length, String errorMessage) {
9 | this.length = length;
10 | this.errorMessage = errorMessage;
11 | }
12 |
13 | @Override
14 | public String errorMessage() {
15 | return errorMessage;
16 | }
17 |
18 | @Override
19 | public boolean isValid(String input) {
20 | return input.length() >= length;
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/library/src/main/java/com/alexfu/formvalidator/rules/RegexRule.java:
--------------------------------------------------------------------------------
1 | package com.alexfu.formvalidator.rules;
2 |
3 | import java.util.regex.Pattern;
4 |
5 | public class RegexRule implements ValidationRule {
6 | private final Pattern pattern;
7 | private String errorMessage;
8 |
9 | public RegexRule(Pattern pattern, String errorMessage) {
10 | this.pattern = pattern;
11 | this.errorMessage = errorMessage;
12 | }
13 |
14 | @Override
15 | public String errorMessage() {
16 | return errorMessage;
17 | }
18 |
19 | @Override
20 | public boolean isValid(String input) {
21 | return pattern.matcher(input).matches();
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/library/src/main/java/com/alexfu/formvalidator/rules/ValidationRule.java:
--------------------------------------------------------------------------------
1 | package com.alexfu.formvalidator.rules;
2 |
3 | public interface ValidationRule {
4 | String errorMessage();
5 | boolean isValid(String input);
6 | }
7 |
--------------------------------------------------------------------------------
/library/src/main/java/com/alexfu/formvalidator/utils/Patterns.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2007 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.alexfu.formvalidator.utils;
17 | import java.util.regex.Matcher;
18 | import java.util.regex.Pattern;
19 | /**
20 | * Commonly used regular expression patterns.
21 | */
22 | public class Patterns {
23 | /**
24 | * Regular expression to match all IANA top-level domains.
25 | * List accurate as of 2011/07/18. List taken from:
26 | * http://data.iana.org/TLD/tlds-alpha-by-domain.txt
27 | * This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
28 | *
29 | * @deprecated Due to the recent profileration of gTLDs, this API is
30 | * expected to become out-of-date very quickly. Therefore it is now
31 | * deprecated.
32 | */
33 | @Deprecated
34 | public static final String TOP_LEVEL_DOMAIN_STR =
35 | "((aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
36 | + "|(biz|b[abdefghijmnorstvwyz])"
37 | + "|(cat|com|coop|c[acdfghiklmnoruvxyz])"
38 | + "|d[ejkmoz]"
39 | + "|(edu|e[cegrstu])"
40 | + "|f[ijkmor]"
41 | + "|(gov|g[abdefghilmnpqrstuwy])"
42 | + "|h[kmnrtu]"
43 | + "|(info|int|i[delmnoqrst])"
44 | + "|(jobs|j[emop])"
45 | + "|k[eghimnprwyz]"
46 | + "|l[abcikrstuvy]"
47 | + "|(mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
48 | + "|(name|net|n[acefgilopruz])"
49 | + "|(org|om)"
50 | + "|(pro|p[aefghklmnrstwy])"
51 | + "|qa"
52 | + "|r[eosuw]"
53 | + "|s[abcdeghijklmnortuvyz]"
54 | + "|(tel|travel|t[cdfghjklmnoprtvwz])"
55 | + "|u[agksyz]"
56 | + "|v[aceginu]"
57 | + "|w[fs]"
58 | + "|(\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\u0441\u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628\u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a|\u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e|\u092d\u093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd|\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0dbd\u0d82\u0d9a\u0dcf|\u0e44\u0e17\u0e22|\u30c6\u30b9\u30c8|\u4e2d\u56fd|\u4e2d\u570b|\u53f0\u6e7e|\u53f0\u7063|\u65b0\u52a0\u5761|\u6d4b\u8bd5|\u6e2c\u8a66|\u9999\u6e2f|\ud14c\uc2a4\ud2b8|\ud55c\uad6d|xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-3e0b707e|xn\\-\\-45brj9c|xn\\-\\-80akhbyknj4f|xn\\-\\-90a3ac|xn\\-\\-9t4b11yi5a|xn\\-\\-clchc0ea0b2g2a9gcd|xn\\-\\-deba0ad|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\-g6w251d|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-j6w193g|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgberp4a5d4ar|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\\-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-s9brj9c|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zckzah|xxx)"
59 | + "|y[et]"
60 | + "|z[amw])";
61 | /**
62 | * Regular expression pattern to match all IANA top-level domains.
63 | * @deprecated This API is deprecated. See {@link #TOP_LEVEL_DOMAIN_STR}.
64 | */
65 | @Deprecated
66 | public static final Pattern TOP_LEVEL_DOMAIN =
67 | Pattern.compile(TOP_LEVEL_DOMAIN_STR);
68 | /**
69 | * Regular expression to match all IANA top-level domains for WEB_URL.
70 | * List accurate as of 2011/07/18. List taken from:
71 | * http://data.iana.org/TLD/tlds-alpha-by-domain.txt
72 | * This pattern is auto-generated by frameworks/ex/common/tools/make-iana-tld-pattern.py
73 | *
74 | * @deprecated This API is deprecated. See {@link #TOP_LEVEL_DOMAIN_STR}.
75 | */
76 | @Deprecated
77 | public static final String TOP_LEVEL_DOMAIN_STR_FOR_WEB_URL =
78 | "(?:"
79 | + "(?:aero|arpa|asia|a[cdefgilmnoqrstuwxz])"
80 | + "|(?:biz|b[abdefghijmnorstvwyz])"
81 | + "|(?:cat|com|coop|c[acdfghiklmnoruvxyz])"
82 | + "|d[ejkmoz]"
83 | + "|(?:edu|e[cegrstu])"
84 | + "|f[ijkmor]"
85 | + "|(?:gov|g[abdefghilmnpqrstuwy])"
86 | + "|h[kmnrtu]"
87 | + "|(?:info|int|i[delmnoqrst])"
88 | + "|(?:jobs|j[emop])"
89 | + "|k[eghimnprwyz]"
90 | + "|l[abcikrstuvy]"
91 | + "|(?:mil|mobi|museum|m[acdeghklmnopqrstuvwxyz])"
92 | + "|(?:name|net|n[acefgilopruz])"
93 | + "|(?:org|om)"
94 | + "|(?:pro|p[aefghklmnrstwy])"
95 | + "|qa"
96 | + "|r[eosuw]"
97 | + "|s[abcdeghijklmnortuvyz]"
98 | + "|(?:tel|travel|t[cdfghjklmnoprtvwz])"
99 | + "|u[agksyz]"
100 | + "|v[aceginu]"
101 | + "|w[fs]"
102 | + "|(?:\u03b4\u03bf\u03ba\u03b9\u03bc\u03ae|\u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435|\u0440\u0444|\u0441\u0440\u0431|\u05d8\u05e2\u05e1\u05d8|\u0622\u0632\u0645\u0627\u06cc\u0634\u06cc|\u0625\u062e\u062a\u0628\u0627\u0631|\u0627\u0644\u0627\u0631\u062f\u0646|\u0627\u0644\u062c\u0632\u0627\u0626\u0631|\u0627\u0644\u0633\u0639\u0648\u062f\u064a\u0629|\u0627\u0644\u0645\u063a\u0631\u0628|\u0627\u0645\u0627\u0631\u0627\u062a|\u0628\u06be\u0627\u0631\u062a|\u062a\u0648\u0646\u0633|\u0633\u0648\u0631\u064a\u0629|\u0641\u0644\u0633\u0637\u064a\u0646|\u0642\u0637\u0631|\u0645\u0635\u0631|\u092a\u0930\u0940\u0915\u094d\u0937\u093e|\u092d\u093e\u0930\u0924|\u09ad\u09be\u09b0\u09a4|\u0a2d\u0a3e\u0a30\u0a24|\u0aad\u0abe\u0ab0\u0aa4|\u0b87\u0ba8\u0bcd\u0ba4\u0bbf\u0baf\u0bbe|\u0b87\u0bb2\u0b99\u0bcd\u0b95\u0bc8|\u0b9a\u0bbf\u0b99\u0bcd\u0b95\u0baa\u0bcd\u0baa\u0bc2\u0bb0\u0bcd|\u0baa\u0bb0\u0bbf\u0b9f\u0bcd\u0b9a\u0bc8|\u0c2d\u0c3e\u0c30\u0c24\u0c4d|\u0dbd\u0d82\u0d9a\u0dcf|\u0e44\u0e17\u0e22|\u30c6\u30b9\u30c8|\u4e2d\u56fd|\u4e2d\u570b|\u53f0\u6e7e|\u53f0\u7063|\u65b0\u52a0\u5761|\u6d4b\u8bd5|\u6e2c\u8a66|\u9999\u6e2f|\ud14c\uc2a4\ud2b8|\ud55c\uad6d|xn\\-\\-0zwm56d|xn\\-\\-11b5bs3a9aj6g|xn\\-\\-3e0b707e|xn\\-\\-45brj9c|xn\\-\\-80akhbyknj4f|xn\\-\\-90a3ac|xn\\-\\-9t4b11yi5a|xn\\-\\-clchc0ea0b2g2a9gcd|xn\\-\\-deba0ad|xn\\-\\-fiqs8s|xn\\-\\-fiqz9s|xn\\-\\-fpcrj9c3d|xn\\-\\-fzc2c9e2c|xn\\-\\-g6w251d|xn\\-\\-gecrj9c|xn\\-\\-h2brj9c|xn\\-\\-hgbk6aj7f53bba|xn\\-\\-hlcj6aya9esc7a|xn\\-\\-j6w193g|xn\\-\\-jxalpdlp|xn\\-\\-kgbechtv|xn\\-\\-kprw13d|xn\\-\\-kpry57d|xn\\-\\-lgbbat1ad8j|xn\\-\\-mgbaam7a8h|xn\\-\\-mgbayh7gpa|xn\\-\\-mgbbh1a71e|xn\\-\\-mgbc0a9azcg|xn\\-\\-mgberp4a5d4ar|xn\\-\\-o3cw4h|xn\\-\\-ogbpf8fl|xn\\-\\-p1ai|xn\\-\\-pgbs0dh|xn\\-\\-s9brj9c|xn\\-\\-wgbh1c|xn\\-\\-wgbl6a|xn\\-\\-xkc2al3hye2a|xn\\-\\-xkc2dl3a5ee0h|xn\\-\\-yfro4i67o|xn\\-\\-ygbi2ammx|xn\\-\\-zckzah|xxx)"
103 | + "|y[et]"
104 | + "|z[amw]))";
105 | /**
106 | * Good characters for Internationalized Resource Identifiers (IRI).
107 | * This comprises most common used Unicode characters allowed in IRI
108 | * as detailed in RFC 3987.
109 | * Specifically, those two byte Unicode characters are not included.
110 | */
111 | public static final String GOOD_IRI_CHAR =
112 | "a-zA-Z0-9\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
113 | public static final Pattern IP_ADDRESS
114 | = Pattern.compile(
115 | "((25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(25[0-5]|2[0-4]"
116 | + "[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]"
117 | + "[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}"
118 | + "|[1-9][0-9]|[0-9]))");
119 | /**
120 | * RFC 1035 Section 2.3.4 limits the labels to a maximum 63 octets.
121 | */
122 | private static final String IRI
123 | = "[" + GOOD_IRI_CHAR + "]([" + GOOD_IRI_CHAR + "\\-]{0,61}[" + GOOD_IRI_CHAR + "]){0,1}";
124 | private static final String GOOD_GTLD_CHAR =
125 | "a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF";
126 | private static final String GTLD = "[" + GOOD_GTLD_CHAR + "]{2,63}";
127 | private static final String HOST_NAME = "(" + IRI + "\\.)+" + GTLD;
128 | public static final Pattern DOMAIN_NAME
129 | = Pattern.compile("(" + HOST_NAME + "|" + IP_ADDRESS + ")");
130 | /**
131 | * Regular expression pattern to match most part of RFC 3987
132 | * Internationalized URLs, aka IRIs. Commonly used Unicode characters are
133 | * added.
134 | */
135 | public static final Pattern WEB_URL = Pattern.compile(
136 | "((?:(http|https|Http|Https|rtsp|Rtsp):\\/\\/(?:(?:[a-zA-Z0-9\\$\\-\\_\\.\\+\\!\\*\\'\\(\\)"
137 | + "\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-zA-Z0-9\\$\\-\\_"
138 | + "\\.\\+\\!\\*\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?"
139 | + "(?:" + DOMAIN_NAME + ")"
140 | + "(?:\\:\\d{1,5})?)" // plus option port number
141 | + "(\\/(?:(?:[" + GOOD_IRI_CHAR + "\\;\\/\\?\\:\\@\\&\\=\\#\\~" // plus option query params
142 | + "\\-\\.\\+\\!\\*\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?"
143 | + "(?:\\b|$)"); // and finally, a word boundary or end of
144 | // input. This is to stop foo.sure from
145 | // matching as foo.su
146 | public static final Pattern EMAIL_ADDRESS
147 | = Pattern.compile(
148 | "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
149 | "\\@" +
150 | "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
151 | "(" +
152 | "\\." +
153 | "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
154 | ")+"
155 | );
156 | /**
157 | * This pattern is intended for searching for things that look like they
158 | * might be phone numbers in arbitrary text, not for validating whether
159 | * something is in fact a phone number. It will miss many things that
160 | * are legitimate phone numbers.
161 | *
162 | * The pattern matches the following:
163 | *
164 | * - Optionally, a + sign followed immediately by one or more digits. Spaces, dots, or dashes
165 | * may follow.
166 | *
- Optionally, sets of digits in parentheses, separated by spaces, dots, or dashes.
167 | *
- A string starting and ending with a digit, containing digits, spaces, dots, and/or dashes.
168 | *
169 | */
170 | public static final Pattern PHONE
171 | = Pattern.compile( // sdd = space, dot, or dash
172 | "(\\+[0-9]+[\\- \\.]*)?" // +*
173 | + "(\\([0-9]+\\)[\\- \\.]*)?" // ()*
174 | + "([0-9][0-9\\- \\.]+[0-9])"); // +
175 | /**
176 | * Convenience method to take all of the non-null matching groups in a
177 | * regex Matcher and return them as a concatenated string.
178 | *
179 | * @param matcher The Matcher object from which grouped text will
180 | * be extracted
181 | *
182 | * @return A String comprising all of the non-null matched
183 | * groups concatenated together
184 | */
185 | public static final String concatGroups(Matcher matcher) {
186 | StringBuilder b = new StringBuilder();
187 | final int numGroups = matcher.groupCount();
188 | for (int i = 1; i <= numGroups; i++) {
189 | String s = matcher.group(i);
190 | if (s != null) {
191 | b.append(s);
192 | }
193 | }
194 | return b.toString();
195 | }
196 | /**
197 | * Convenience method to return only the digits and plus signs
198 | * in the matching string.
199 | *
200 | * @param matcher The Matcher object from which digits and plus will
201 | * be extracted
202 | *
203 | * @return A String comprising all of the digits and plus in
204 | * the match
205 | */
206 | public static final String digitsAndPlusOnly(Matcher matcher) {
207 | StringBuilder buffer = new StringBuilder();
208 | String matchingRegion = matcher.group();
209 | for (int i = 0, size = matchingRegion.length(); i < size; i++) {
210 | char character = matchingRegion.charAt(i);
211 | if (character == '+' || Character.isDigit(character)) {
212 | buffer.append(character);
213 | }
214 | }
215 | return buffer.toString();
216 | }
217 | /**
218 | * Do not create this static utility class.
219 | */
220 | private Patterns() {}
221 | }
--------------------------------------------------------------------------------
/library/src/test/java/com/alexfu/formvalidator/EmailRuleTests.kt:
--------------------------------------------------------------------------------
1 | package com.alexfu.formvalidator
2 |
3 | import com.alexfu.formvalidator.rules.EmailRule
4 | import org.jetbrains.spek.api.Spek
5 | import kotlin.test.assertFalse
6 | import kotlin.test.assertTrue
7 |
8 | class EmailRuleTests : Spek({
9 |
10 | describe("email rule") {
11 | val emailRule = EmailRule("Invalid email.")
12 |
13 | it("should return false for a bad email") {
14 | val result = emailRule.isValid("badEmail@gmail")
15 | assertFalse(result)
16 | }
17 |
18 | it("should return true for a good email") {
19 | val result = emailRule.isValid("goodEmail@gmail.com")
20 | assertTrue(result)
21 | }
22 | }
23 |
24 | })
25 |
--------------------------------------------------------------------------------
/library/src/test/java/com/alexfu/formvalidator/MinLengthRuleTests.kt:
--------------------------------------------------------------------------------
1 | package com.alexfu.formvalidator
2 |
3 | import com.alexfu.formvalidator.rules.MinLengthRule
4 | import org.jetbrains.spek.api.Spek
5 | import kotlin.test.assertFalse
6 | import kotlin.test.assertTrue
7 |
8 | class MinLengthRuleTests : Spek({
9 |
10 | describe("min length rule") {
11 | val rule = MinLengthRule(5, "Must be at least 5 characters long.")
12 |
13 | it("should return true for good input") {
14 | val result = rule.isValid("12345")
15 | assertTrue(result)
16 | }
17 |
18 | it("should return false for bad input") {
19 | val result = rule.isValid("123")
20 | assertFalse(result)
21 | }
22 | }
23 |
24 | })
--------------------------------------------------------------------------------
/library/src/test/java/com/alexfu/formvalidator/RegexRuleTests.kt:
--------------------------------------------------------------------------------
1 | package com.alexfu.formvalidator
2 |
3 | import com.alexfu.formvalidator.rules.RegexRule
4 | import org.jetbrains.spek.api.Spek
5 | import java.util.regex.Pattern
6 | import kotlin.test.assertFalse
7 | import kotlin.test.assertTrue
8 |
9 | class RegexRuleTests : Spek({
10 |
11 | describe("regex rule") {
12 | val rule = RegexRule(Pattern.compile("[0-9]+"), "No match.")
13 |
14 | it("should return true with good input") {
15 | val result = rule.isValid("123")
16 | assertTrue(result)
17 | }
18 |
19 | it("should return true with bad input") {
20 | val result = rule.isValid("123ABC")
21 | assertFalse(result)
22 | }
23 | }
24 |
25 | })
26 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':demo', ':library'
2 |
--------------------------------------------------------------------------------