如何在android中验证电子邮件ID?

时间:2016-08-30 09:23:37

标签: android

我已经在我的应用程序中实现了电子邮件验证,但问题是应用程序接受超过64个字符的本地域部分,当我写..nnn .. @ gmail.com应用程序也接受这个以及根据RFC 3696规范#section 3,句点(“。”)不得用于开始或结束本地部分,也不得出现两个或多个连续句点。

请帮帮我

java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy7.findElement(Unknown Source)
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy9.isDisplayed(Unknown Source)
at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:302)
at org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:41)
at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:288)
at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:285)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:238)
at com.multicom.fabrix.framework.TestBase.waitFor(TestBase.java:152)
at com.multicom.fabrix.framework.TestBase.waitForIsDisplayed(TestBase.java:141)
at com.multicom.fabrix.pageobjects.CustomerHomePage.waitForResults(CustomerHomePage.java:75)
at com.multicom.fabrix.regressiontests.FlightBookingTest.searchForAPackage(FlightBookingTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.mycompany.mymodule.webdriver.WebDriverInvoker.invokeNormally(WebDriverInvoker.java:47)
at com.mycompany.mymodule.webdriver.WebDriverInvoker.invoke(WebDriverInvoker.java:38)
... 41 more

7 个答案:

答案 0 :(得分:5)

Android SDK有一个内置的正则表达式,用于检查电子邮件。

Patterns JavaDoc

Matcher matcher = Patterns.EMAIL_ADDRESS.matcher(email);
return matcher.matches();

答案 1 :(得分:3)

试试这段代码。如果电子邮件有效,则返回true。

android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches();

答案 2 :(得分:2)

public final static boolean isValidEmail(CharSequence target) {
    if (TextUtils.isEmpty(target)) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}

答案 3 :(得分:1)

试试这个,

String expression = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";

答案 4 :(得分:1)

尝试这个

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

public class EmailValidation {
private Pattern pattern;
private Matcher matcher;

private static final String EMAIL_PATTERN = 
    "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
    + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

public EmailValidation() {
    pattern = Pattern.compile(EMAIL_PATTERN);
}

public boolean isValidate(final String hex) {
    matcher = pattern.matcher(hex);
    return matcher.matches();
  }
}

答案 5 :(得分:1)

我建议在所有这些答案中提供一个简单的解决方案

public static boolean isEmail(String email){
  return android.util.Patterns.EMAIL_ADDRESS.matcher(inputData.trim()).matches()
}

阅读有关模式的this文档。

答案 6 :(得分:0)

我建议您查看此库以进行Android客户端验证

https://github.com/ragunathjawahar/android-saripaar

相关问题