1

I have tried a lot for this validation but I'm not getting where to put the following code

String expression="^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";.

Please help me in this.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Vaibhav Aralkar
  • 61
  • 2
  • 2
  • 6
  • The reliable way to validate an email address is to send an email and ask for a reply. – Andrew Thompson Mar 07 '13 at 11:06
  • @Andrew Thompson but in a textfield how to validate the email id with regex? – Vaibhav Aralkar Mar 07 '13 at 11:08
  • in a information page i have to validate & m developing a desktop app. – Vaibhav Aralkar Mar 07 '13 at 11:11
  • When do you want to check the JTextField? Only when the user clicks a validate button or when he leaves the JTextField? – Rob Mar 07 '13 at 11:15
  • @Rob when he clickes submit button it should check whether the entered email id is in correct format or not? I dont bother about the existence of the email id. – Vaibhav Aralkar Mar 07 '13 at 11:16
  • Take a look at [this question](http://stackoverflow.com/q/1313390/1076463) and [my answer containing example code](http://stackoverflow.com/a/13424140/1076463). That is for numeric input but you can easily write your own `java.text.Format` based on that regex to accept only emails – Robin Mar 07 '13 at 11:20

2 Answers2

9

I hope this will help. When you Click on Submit button on SWING UI, in actionListener write the validation code.

   Emailvalidator emailValidator = new Emailvalidator();
   if(!emailValidator.validate(emailField.getText().trim())) {
        System.out.print("Invalid Email ID");
        /*
           Action that you want to take. For ex. make email id field red
           or give message box saying invalid email id.
        */
   }

This is the seperate class EmailValidator

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

public class EmailValidator {

    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 EmailValidator() {
        pattern = Pattern.compile(EMAIL_PATTERN);
    }

    /**
     * Validate hex with regular expression
     * 
     * @param hex
     *            hex for validation
     * @return true valid hex, false invalid hex
     */
    public boolean validate(final String hex) {

        matcher = pattern.matcher(hex);
        return matcher.matches();

    }
}

On a side note, this will enable you that the entered email id is in the correct format. If you want to validate the id, send an email containing a confirmation no and ask the user to enter that number on your UI and then proceed.

ragingasiancoder
  • 616
  • 6
  • 17
Vallabh Patade
  • 4,960
  • 6
  • 31
  • 40
  • for this whether i have to create a new class or in the source panel of my Design panel i can write? – Vaibhav Aralkar Mar 07 '13 at 11:18
  • Its good practise as you may require to validate email id on multiple screen. You may write this as a seperate utility class, and use it as and when required. – Vallabh Patade Mar 07 '13 at 11:19
  • ok thanks. I will try this code. But in the buttonevent i have to check the entered email id is correct or not? So for that what shoul i do? – Vaibhav Aralkar Mar 07 '13 at 11:21
0

The format of an e-mail address can get pretty complex. In fact, its complexity is almost legendary.

The InternetAddress class in JavaMail is a much more reliable way to parse an e-mail address than a regular expression.

VGR
  • 40,506
  • 4
  • 48
  • 63