How to validate 'email' using Regular Expression in Java with NetBeansIDE


How to validate 'email' using Regular Expression in Java with NetBeansIDE

1. Open NetBeansIDE

2. In your project Make a java form(e.g. JFrame Form)

   2.1 Design this Form with one JTextField and a button in it.

3. Make Validate.java class
   
   3.1 In this class make a static method with code:

       public static boolean validateEmail(String email) {                                        
       boolean status=false;   
       String EMAIL_PATTERN =
        "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
        + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
       Pattern pattern = Pattern.compile(EMAIL_PATTERN);
       Matcher matcher=pattern.matcher(email);
       if(matcher.matches())
       {
           status=true;
       }
       else{
           status=false;
       }
           return status;
           
    }

4. Right Click on Button and Goto Events~Action~actionPerformed

   4.1 In this method Call the 'validateEmail' method of Validate class           as:
      
       boolean status=Validate.validateEmail(jTextField1.getText());
       if(status){
           jLabel2.setText("Email Valid");
       }else{
           jLabel2.setText("Not Valid Email");
       }

5. Run your project with JForm.

6. Check the Validation.

7. Finish

Thank You :)

Facebook updates

Comments

Popular posts from this blog

How to Perform DES Encryption and Decryption of TXT file in JAVA with NetBeansIDE

How to Detect CapsLock key using Java in NetBeansIDE.

How to insert Image in MySQL Database with Java in NetBeansIDE