2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.web.generator.client;
20 * Helpers methods to check for phone numbers, email addresses, and URL. Other
21 * general purpose check methods should go here as well.
23 * @author Yohann Coppel
25 public final class Validators {
26 public static String filterNumber(String number) {
27 return number.replaceAll("[ \\.,\\-\\(\\)]", "");
30 public static void validateNumber(String number) throws GeneratorException {
31 if (!number.matches("\\+?[0-9]+")) {
32 throw new GeneratorException("Phone number must be digits only.");
36 public static void validateUrl(String url) throws GeneratorException {
37 if (!isBasicallyValidURI(url)) {
38 throw new GeneratorException("URL is not valid.");
42 private static boolean isBasicallyValidURI(String uri) {
43 if (uri == null || uri.indexOf(' ') >= 0 || uri.indexOf('\n') >= 0) {
46 int period = uri.indexOf('.');
47 // Look for period in a domain but followed by at least a two-char TLD
48 if (period >= uri.length() - 2) {
51 return period >= 0 || uri.indexOf(':') >= 0;
54 public static void validateEmail(String email) throws GeneratorException {
55 //FIXME: we can have a better check for email here.
56 if (!email.matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$")) {
57 throw new GeneratorException("Email is not valid.");