Results 1 to 4 of 4

Thread: Validating Email in Java

  1. #1
    Join Date
    Sep 2009
    Posts
    177

    Validating Email in Java

    I have a question about validation. I am developing a project in Java. For this project I am not able to get the Email entered to be Validated. I need to validate the email so that on a valid email can be accepted. What criteria should be taken into account while validating an Email in Java. Can anyone suggest a suitable solution ?

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Validating Email in Java

    Well I can help you with the criteria for validation of email. But try to code it youself. An email should always start with either an alphabet or a number. It can contain A-Z or a-z or numerics 0-9 , an Underscore "_" and now-a-days even a period "." It should contain an @ symbol and a valid domain name. A simple example would be: emailidname@emailprovidername.domain name No spaces are allowed. Also the following are not valid ( ) ; < > @ : , \ [ ]

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Validating Email in Java

    Validating an email is not an easy task in java. There are various conditions to be taken care of. Try running this code:



    Code:
    package mypck.regex;
     
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
     
    public class ValidEmail
     {
     
    	  private Pattern pat;
    	  private Matcher mat;
     
    	  private static final String epat = 
       "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
     
    	  public ValidEmail()
                   {
    		  pat = Pattern.compile(epat);
    	   }
     	 
    	  public boolean validate(final String hex){
     
    		  mat = pat.mat(hex);
    		  return mat.matches();
     
    	  }
    }
    Last edited by Katty; 04-11-2009 at 05:12 PM.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Validating Email in Java

    I also had a same issue when I had to do it for the first time. Here is code that can help you to validate email in Java programming language.


    Code:
    package temp;
    
    
    public class valmail
    {
    private String eid="";
    
    public valmail(String eid)
    {
     this.eid = eid;
    }
    
    public boolean chk()
    {
     if (eid == null)
     return false;
     
     if(eid.equals(""))
     return false;
     
     int pos1 = eid.indexOf("@");
     int pos2 = eid.indexOf("@",pos1+1);
    
     if (pos2>=0)
     return false;
     
     int pos1a = eid.indexOf(".");
     int pos2a = eid.indexOf(".",pos1a+1);
    
     if (pos2a>=0)
     return false;
     
     return true;
    }
    
    }


    You can edit the code to fit to your project's requirement. Best of Luck.

Similar Threads

  1. Why my wireless is asking validating identity
    By Krishanu in forum Networking & Security
    Replies: 6
    Last Post: 16-10-2010, 02:46 PM
  2. Validating Data in Java
    By Ucchal in forum Software Development
    Replies: 5
    Last Post: 10-08-2010, 10:42 AM
  3. How to send an automatic email in java?
    By Parvati in forum Software Development
    Replies: 6
    Last Post: 27-07-2010, 02:42 PM
  4. How to do Validating and Sanitizing in PHP?
    By michaels in forum Software Development
    Replies: 4
    Last Post: 23-02-2010, 06:10 AM
  5. Send Email With Java
    By Kushan in forum Software Development
    Replies: 2
    Last Post: 24-03-2009, 03:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,572,398.02218 seconds with 16 queries