is Valid Email - Android java.util.regex

Android examples for java.util.regex:Email Pattern

Description

is Valid Email

Demo Code

import java.net.MalformedURLException;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    /**/*www. ja v  a  2s.c o m*/
     * 
     * <pre>
     * StringUtil.isValidEmail(null)          = false;
     * StringUtil.isValidEmail("abc.abc")     = false;
     * StringUtil.isValidEmail("abc@abc.com") = true;
     * 
     * @param inputStr
     * @return
     */
    public static boolean isValidEmail(String inputStr) {
        boolean rtn = false;

        if (inputStr == null) {
            return rtn;
        }

        String regex = "^[_a-z0-9-]+(.[_a-z0-9-]+)*@(?:\\w+\\.)+\\w+$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(inputStr);

        if (m.matches()) {
            rtn = true;
        }

        return rtn;
    }

}

Related Tutorials