0

I have a form and among the fields to fill is the mail I want to make a control so that the input address is consistent with the standard mails

responsableTechnique.setMail(mailResponsable.getText());
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Daly
  • 19
  • 8
  • You can use a regular expression to check if the string/email address matches a pattern see http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/ – Mike Holtkamp Apr 28 '16 at 12:16
  • See [How to Use the Focus Subsystem: Validating Input](https://docs.oracle.com/javase/tutorial/uiswing/misc/focus.html#inputVerification). – Andrew Thompson Apr 28 '16 at 12:28
  • 1
    You may also read http://www.regular-expressions.info/email.html to understand why validating email through regexp is an hard task... – Jean-Baptiste Yunès Apr 28 '16 at 12:29

1 Answers1

0

You can with regex, example :

public static void main(String[]args){
    String regex = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$";

    Pattern.matches(regex,"test@test.fr"); //True
    Pattern.matches(regex,"test.test@test.fr"); //True
    Pattern.matches(regex,"testtest.fr"); //False
    Pattern.matches(regex,"test.test@testfr"); //False
    Pattern.matches(regex,"test@test."); //False
    Pattern.matches(regex,"test@te@st.fr"); //False

}

So for your example :

String regex = "^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)+$";
    String mail = mailResponsable.getText();

    if(Pattern.matches(regex,mail)) responsableTechnique.setMail(mail);