Compares two text entry boxes to make sure the contents of one is a subdomain of the other - Android User Interface

Android examples for User Interface:EditText

Description

Compares two text entry boxes to make sure the contents of one is a subdomain of the other

Demo Code

/*//ww w . j a  v  a  2 s.co  m
 * Copyright 2013 ForgeRock AS.
 *
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions copyright [year] [name of copyright owner]".
 */
import android.content.Context;
import android.net.Uri;
import android.widget.EditText;
import java.net.MalformedURLException;
import java.net.URL;

public class Main{
    /**
     * Compares two text entry boxes to make sure the contents of one
     * is a subdomain of the other
     *
     * @param context The actvity this validation was called from
     * @param domainT The (sub)domain which to check
     * @param hostT The host domain of which the subdomain must be a part
     * @return true if domainT is a subdomain of hostT of the type ".subdomain.toplevel"
     */
    public static boolean validateSubdomain(Context context,
            EditText domainT, EditText hostT) {
        String domain = domainT.getText().toString();
        String host = hostT.getText().toString();

        Uri openam = Uri.parse(host);

        //empty or null
        if (domain == null || domain.equals("")) {
            return true;
        }

        //matches subdomain, includes at least two dots and starts with one of them
        if (openam.getHost().endsWith(domain)
                && domain.matches("^\\..+\\..+")) {
            return true;
        }

        AndroidUtils.showToast(domainT.getHint(), context);

        return false;
    }
}

Related Tutorials