001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: TelephoneValidator.java,v 1.3 2008/06/25 05:41:48 qcheng Exp $
026 *
027 */
028
029/**
030 * Portions Copyrighted [2011] [ForgeRock AS]
031 */
032package com.iplanet.ums.validation;
033
034/**
035 * Validates telephone number The object of this class is constructed using
036 * default constructor and validate function is used to validate telephone
037 * number passing the telephone number as argument to the function. Upon
038 * validation the validate function returns boolean value.
039 * @supported.all.api
040 */
041public class TelephoneValidator implements IValidator {
042
043    /**
044     * Determines if the value is a valid "basic" telephone number string
045     * 
046     * @param value
047     *            string value to validate
048     * @param rule
049     *            not used by this method
050     * @return true if the value represents a valid telephone number string
051     */
052    public boolean validate(String value, String rule) {
053        return validate(value);
054    }
055
056    /**
057     * Determines whether the specified string is a valid "basic" telephone
058     * number string
059     * 
060     * @param telephone
061     *            string telephone to validate
062     * @return true if telephone is a valid telephone number string
063     */
064    public boolean validate(String telephone) {
065        char aChar;
066
067        StringBuilder buf = new StringBuilder(telephone);
068
069        for (int aIndex = 0; aIndex < buf.length(); aIndex++) {
070            aChar = buf.charAt(aIndex);
071
072            if (!Character.isSpaceChar(aChar)) {
073                if (!Character.isDigit(aChar) && !isValidTelephoneChars(aChar)) 
074                {
075                    return false;
076                }
077            }
078        }
079
080        return true;
081    }
082
083    /**
084     * Determines whether the specified string is a valid telephone character
085     * like: ()-.
086     */
087    private boolean isValidTelephoneChars(char theChar) {
088        int aVal = theChar;
089
090        // check if value is
091        // '()-.' in ascii
092        if ((aVal == 40) || (aVal == 41) || (aVal == 45) || (aVal == 46)) {
093            return true;
094        } else {
095            return false;
096        }
097    }
098
099}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.