0

How do I validate an email address?

An address should have a "@ " and end ".com "

Here's the code I used:

public void email(){

    String a = "qwertyuiopasdfghjklzxcqwertyuiopasdfghjklzxcvbnmvbnmqwertyuiopasdfghjklzxcvbnm@qwertyuiopasdfghjklzxcvbnmqwertyuiopaqwertyuiopasdfghjklzxcvbnmsdfghjklzxcvbnm.com";

    String b = JOptionPane.showInputDialog("Enter email");
    try{

        if(!b.contains(a)){

            throw new Error("Incorrect");

        }else{

            System.out.println("Correct");
        }



    } catch (Error e){

        System.out.println(""+e);

    }



}

1 Answers1

0

I think this might help you. From: What is the best Java email address validation method?

public static boolean isValidEmailAddress(String email) {
   boolean result = true;
   try {
      InternetAddress emailAddr = new InternetAddress(email);
      emailAddr.validate();
   } catch (AddressException ex) {
      result = false;
   }
   return result;
}
Community
  • 1
  • 1
jhegeman2
  • 59
  • 6