51646bfb9ff1e287080e2bf367fc6f2d37e8d249
[zxing.git] / zxing.appspot.com / generator / src / com / google / zxing / web / generator / client / Validators.java
1 /*
2  * Copyright (C) 2008 ZXing authors
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
17 package com.google.zxing.web.generator.client;
18
19 /**
20  * Helpers methods to check for phone numbers, email addresses, and URL. Other
21  * general purpose check methods should go here as well.
22  * 
23  * @author Yohann Coppel
24  */
25 public final class Validators {
26   public static String filterNumber(String number) {
27     return number.replaceAll("[ +\\.,\\-\\(\\)]", "");
28   }
29   
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.");
33     }
34   }
35   
36   public static void validateUrl(String url) throws GeneratorException {
37     //FIXME: url specification is a bit more complex than just that.
38     if (!((url.startsWith("http://") && url.length() > 7)
39         || (url.startsWith("https://") && url.length() > 8))) {
40       throw new GeneratorException("URL: http:// or https://," +
41             "plus at least 1 character.");
42     }
43   }
44   
45   public static void validateEmail(String email) throws GeneratorException {
46     //FIXME: we can have a better check for email here.
47     if (!email.matches("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}$")) {
48       throw new GeneratorException("Email is not valid.");
49     }
50   }
51
52 }