java中的电子邮件验证

时间:2016-11-09 10:47:51

标签: java regex

是否有任何正则表达式来验证不允许.com.com的电子邮件,例如example@example.com.com但允许.com.uk? 我粘贴了我的代码,当用户输入.com.com时,该代码应返回false。任何人都可以在下面验证我的代码吗?

String ePattern = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern p =Pattern.compile(ePattern);
Matcher m = p.matcher(studentemail);
b= m.matches();
    if(!b) {
        eMsg.addError("email must be in formate: example@example.xxx / example@example.xx");
        request.setAttribute("errorMessage", eMsg);
        requestDispatcher = request.getRequestDispatcher("/addrecord.jsp");
        requestDispatcher.forward(request, response);
        return;
    }   

     else {
           System.out.println("valid email id");
      } 

1 个答案:

答案 0 :(得分:3)

在大多数情况下,这样的正则表达式不仅限于Java(仅当使用Java正则表达式引擎不支持的特殊功能时)。除此之外,如果你想匹配 @Configuration @EnableJpaRepositories( basePackages = "com.mycompany.common.services", transactionManagerRef = "jpaTransactionManager" ) @EnableTransactionManagement(order = 5) public class PersistenceConfiguration { @Bean @Qualifier(value = "entityManagerFactory") public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource ) { LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean(); factory.setPersistenceUnitName( "entityManagerFactory" ); factory.setPackagesToScan( entityPackages() ); factory.setJpaVendorAdapter( getHibernateJpaVendorAdapter() ); factory.setJpaProperties( getJpaProperties() ); factory.setDataSource( dataSource ); factory.afterPropertiesSet(); return factory; } @Bean @Qualifier(value = "jpaTransactionManager") public PlatformTransactionManager jpaTransactionManager( EntityManagerFactory entityManagerFactory, DataSource dataSource ) { JpaTransactionManager txManager = new JpaTransactionManager(); txManager.setEntityManagerFactory( entityManagerFactory ); txManager.setDataSource( dataSource ); return txManager; } @Bean @Qualifier(value = "sessionFactory") public FactoryBean<SessionFactory> sessionFactory( EntityManagerFactory entityManagerFactory ) { HibernateJpaSessionFactoryBean hibernateJpaSessionFactoryBean = new HibernateJpaSessionFactoryBean(); hibernateJpaSessionFactoryBean.setEntityManagerFactory( entityManagerFactory ); return hibernateJpaSessionFactoryBean; } // How do I remove this and just use the one transaction manager above? /* @Bean @Qualifier(value = "hibernateTransactionManager") @Primary public PlatformTransactionManager hibernateTransactionManager( SessionFactory sessionFactory ) { HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager( sessionFactory ); return hibernateTransactionManager; } */ @Bean public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { return new PersistenceExceptionTranslationPostProcessor(); } protected HibernateJpaVendorAdapter getHibernateJpaVendorAdapter() { HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); vendorAdapter.setGenerateDdl( isGenerateDDL() ); vendorAdapter.setDatabase( Database.MYSQL ); vendorAdapter.setDatabasePlatform( com.mycompany.common.utils.hibernate.MySQL56InnoDBDialect.class.getName() ); return vendorAdapter; } protected Properties getJpaProperties() { Properties properties = new Properties(); properties.put("hibernate.current_session_context_class", "org.springframework.orm.hibernate5.SpringSessionContext"); properties.put("hibernate.hbm2ddl.auto", "validate"); properties.put("hibernate.transaction.flush_before_completion", "true"); properties.put("hibernate.transaction.auto_close_session", "false"); properties.put("hibernate.use_outer_join", "true"); properties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"); properties.put("hibernate.cache.use_second_level_cache", "true"); properties.put("net.sf.ehcache.configurationResourceName", "META-INF/resources/ehcache-hibernate.xml"); properties.put("hibernate.cache.use_query_cache", "true"); properties.put("hibernate.jdbc.batch_size", "100"); properties.put("hibernate.generate_statistics", "true"); properties.put("hibernate.format_sql", "true"); properties.put("hibernate.use_sql_comments", "true"); properties.put("org.hibernate.SQL", "info"); return properties; } protected boolean isGenerateDDL() { return false; } } ,你应该调整正则表达式的第二部分(特别是那部分中的第一部分)。但请注意,像@example.xx这样的域可能无效。

同样xxx.co.uk 可能是一个无效的域,但它也可能不是(至少在其他组合中,例如xx.com.com是一个有效域),所以你可能想要允许,仅测试特定组合或限制此类域的使用。

但是,根据您的使用情况,发送确认电子邮件可能会更好,因为正则表达式可以告诉您看起来正确但可能仍然存在错误。