LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   check e-mail address (https://www.linuxquestions.org/questions/programming-9/check-e-mail-address-53346/)

painkiller6.6.6 04-04-2003 01:37 AM

check e-mail address
 
Hi, I'm doing an applet in java where I get users e-mail address. I'm trying to check if the e-mail is right written, something@something.something .Do you know how to do it?, or some webpage that can explain it?.
Thanks in advance.

grub 04-04-2003 04:04 AM

Hi painkiller, I think for solutions you have to search on any java site or java documentation.:D

FredrikN 04-04-2003 08:56 AM

Wait some minutes and I can write something for you.......

FredrikN 04-04-2003 09:14 AM

test@linuxquestions.org "is valid"

test@linuxquestionsorg "not valid"

test@linuxquestions@org "not valid"

test.linuxquestions@org "not valid"

test@linuxquestions.org. "not valid" (ending with a dot)

and so on......

It's not perfekt 100% but I hope it's better than nothing

Code:

import java.util.*;

public class EmailChecker
{
       
        private int countAt;
        private int countDot;
        private int countRight;
        private int pos;
        private int target;
        private String right;
        private StringTokenizer strTok;
               
        public boolean check(String adress)
        {
               
                               
                for(int j = 0 ; j < adress.length() ; j++)
                {
                        if(adress.charAt(j)=='@')
                                countAt++;
       
                        if(adress.charAt(j)=='.')
                                countDot++;
                               
                }
               
                if((countAt != 1) || (countDot < 1))
                {
                        return false;
                }
               
                //Check so there is something before the @ and after
               
                if((adress.charAt(0) == '@') || (adress.charAt((adress.length())-1) == '@') || (adress.charAt((adress.length())-1) == '.'))
                {
                        return false;
                }
               
                for(int j = 0 ; j < adress.length() ; j++)
                {
                        if(adress.charAt(j)=='@')
                                target = pos++;
       
                        pos++;       
                }               
               
                if(target < adress.length()+1)
                {
               
                        if((adress.charAt(target+1) == '.') || (adress.charAt(target+2) == '.'))
                        {
                                return false;
                        }
                }       
               
                strTok = new StringTokenizer(adress,"@");
               
                while(strTok.hasMoreTokens())
                {
                        right = strTok.nextToken();
                }
               
                //Checking for a . after @
                for(int j = 0 ; j < right.length() ; j++)
                {
                        if(right.charAt(j)=='.')
                                countRight++;
                }               
               
                if(countRight < 1)
                {
                        return false;
                }
       
               
                if(right.length() < 3)
                {
                        return false;
                }
               
                       
                return true;
        }
               
       
}


mukul_joshi 04-08-2003 12:32 AM

Email Validation
 
Check out this Site http://www.network-tools.com it has got a Email Validation there

Shak 04-11-2003 06:39 AM

I didnt do this in Java but I did it in PHP, the way that I did it was to make sure that it was valid by splitting it round the @ sign and checking both of the sections for anything invalid, I then ran the checkdns function on the domain to check that it exists.

Shak

FredrikN 04-11-2003 06:43 AM

I did something similar in PHP to a few months ago and I was also checking with DNS if the domain existed.

But in an performences point of view it's a bad idé.

It all depends on how fast you want the 'Email checking' to be

Shak 04-11-2003 06:51 AM

Thats a fair point, it depends what its for, if its for something really important then its worth taking the time out to do it :)

Shak

FredrikN 04-11-2003 06:53 AM

That's true

painkiller6.6.6 06-13-2003 02:30 AM

Thanks a lot. Sorry for answering so so late, but I had some problems, bike crash, and I wasn´t in the mood of programming, now everything is OK and I'm back.

Looking_Lost 06-13-2003 06:19 AM

You can do regular expression checking the same sort of way as perl does on strings if you just want to test the syntax


stringname.match("<expression")

something you could look into I guess if you wanted.


All times are GMT -5. The time now is 05:25 AM.