Returns true if the given string is an email. - Android java.util.regex

Android examples for java.util.regex:Email Pattern

Description

Returns true if the given string is an email.

Demo Code

/*/*w  ww. j  ava  2 s  .  co  m*/
 * Copyright 2013, Green Halo Labs LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License.
 * You may obtain a copy of the License in the LICENSE file, or at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
//package com.java2s;

import android.annotation.SuppressLint;

import android.text.TextUtils;
import android.util.Patterns;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Returns true if the given string is an email.
     */
    @SuppressLint("NewApi")
    private static boolean isEmail(String account) {

        if (TextUtils.isEmpty(account)) {
            return false;
        }

        final Pattern emailPattern = Patterns.EMAIL_ADDRESS;
        final Matcher matcher = emailPattern.matcher(account);
        return matcher.matches();
    }
}

Related Tutorials