Help me check String -- mail address

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nguyenlh
    New Member
    • Mar 2007
    • 25

    Help me check String -- mail address

    in java I want to check A String as mail
    I have done but i think itisn't true
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Originally posted by nguyenlh
    in java I want to check A String as mail
    I have done but i think itisn't true
    Post your Code what you tried so far with Code Tags.
    One more thing do you know regular expression.
    Have a look at this and try it using Regular Expression.
    Mind it, Reg Exp. supported in J2SE-1.5 :-)

    Kind regards,
    Dmjpro.

    Comment

    • nguyenlh
      New Member
      • Mar 2007
      • 25

      #3
      Originally posted by dmjpro
      Post your Code what you tried so far with Code Tags.
      One more thing do you know regular expression.
      Have a look at this and try it using Regular Expression.
      Mind it, Reg Exp. supported in J2SE-1.5 :-)

      Kind regards,
      Dmjpro.
      class StringDemo{
      public static boolean isEmailAddress( String address) {
      int dot,at;
      dot = address.indexOf (".");
      at = address.indexOf ("@");
      if( at ==-1 ||dot==-1|| address.length( )< dot||(dot<at)&& ) return false;
      else return true;
      }
      public static void main( String[] arg){
      System.out.prin tln(isEmailAddr ess("lenguye..@ "));
      }


      }
      but if string conatin dot before @ i can't process

      Comment

      • nguyenlh
        New Member
        • Mar 2007
        • 25

        #4
        help me !! this probelem i think it isn't difficult to you
        thanks

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by nguyenlh
          class StringDemo{
          public static boolean isEmailAddress( String address) {
          int dot1,at1,dot2,a t2;
          dot = address.indexOf (".");
          at = address.indexOf ("@");
          if( at ==-1 ||dot==-1|| address.length( )< dot||(dot<at)&& ) return false;
          else return true;
          }
          public static void main( String[] arg){
          System.out.prin tln(isEmailAddr ess("lenguye..@ "));
          }


          }
          but if string conatin dot before @ i can't process

          Please use Code Tags.
          So you are checking that only one occurrence of "." and "@".
          Right :-)
          It will be possible with Regular Expression but I am not familiar with Regular Expression, so I am doing it using plain Java Code.

          [code=java]
          public static boolean isEmailAddress( String address) {
          int dot,at;
          dot1 = address.indexOf (".");
          at1 = address.indexOf ("@");
          dot2 = address.lastInd exOf(".");
          at2 = address.lastInd exOf("@");
          if(dot1!=-1 && at1 !=-1 && dot1 != dot2 && at1 != at2) return true;
          else return false;
          }
          [/code]
          Enjoy the code :-)
          Good Luck !

          Kind regards,
          Dmjpro.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Always use a regular expression for this stuff, something like [CODE=java]Pattern.compile (".+@.+\\.[a-z]+");[/CODE]

            Comment

            • nguyenlh
              New Member
              • Mar 2007
              • 25

              #7
              Originally posted by dmjpro
              Please use Code Tags.
              So you are checking that only one occurrence of "." and "@".
              Right :-)
              It will be possible with Regular Expression but I am not familiar with Regular Expression, so I am doing it using plain Java Code.

              [code=java]
              public static boolean isEmailAddress( String address) {
              int dot,at;
              dot1 = address.indexOf (".");
              at1 = address.indexOf ("@");
              dot2 = address.lastInd exOf(".");
              at2 = address.lastInd exOf("@");
              if(dot1!=-1 && at1 !=-1 && dot1 != dot2 && at1 != at2) return true;
              else return false;
              }
              [/code]
              Enjoy the code :-)
              Good Luck !

              Kind regards,
              Dmjpro.
              that code above it can't run true
              it's true when i input 2 dot and 2 @

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by nguyenlh
                that code above it can't run true
                it's true when i input 2 dot and 2 @
                Sorry I did a great mistake, :-(
                Can't you change it yourself buddy, please apply your own brain when you have it :-)
                Anyway !

                [code=java]
                if(dot1!=-1 && at1 !=-1 && dot1 == dot2 && at1 == at2) return true
                else return false;
                [/code]

                Kind regards,
                Dmjpro.

                Comment

                • nguyenlh
                  New Member
                  • Mar 2007
                  • 25

                  #9
                  Originally posted by nguyenlh
                  that code above it can't run true
                  it's true when i input 2 dot and 2 @
                  you can check this code
                  public boolean isEmailAddress( String address) {
                  int At,Dot,DO;
                  At = address.indexOf ("@",0);
                  Dot = address.indexOf (".",At+1);
                  if(address.leng th() > Dot) DO = 1;
                  else = -1;

                  if((At == -1) || (Dot == -1) || (Do == -1)) return false;
                  return true;
                  }

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Originally posted by nguyenlh
                    you can check this code
                    public boolean isEmailAddress( String address) {
                    int At,Dot,DO;
                    At = address.indexOf ("@",0);
                    Dot = address.indexOf (".",At+1);
                    if(address.leng th() > Dot) DO = 1;
                    else = -1;

                    if((At == -1) || (Dot == -1) || (Do == -1)) return false;
                    return true;
                    }
                    What about my code?

                    Kind regards,
                    Dmjpro.

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Throw all that stuff away and use regular expressions. They are the right tool for the job.

                      P.S If you guys really like to learn Java, you have to first learn to take advice.

                      Comment

                      • dmjpro
                        Top Contributor
                        • Jan 2007
                        • 2476

                        #12
                        Originally posted by r035198x
                        Throw all that stuff away and use regular expressions. They are the right tool for the job.

                        P.S If you guys really like to learn Java, you have to first learn to take advice.
                        Yeah it's right, but I think if it is done then he or she can learn the logic development.
                        But finally he or she will do that using Reg Exp.
                        Don't mind Admin, if I told anything wrong.

                        Kind regards,
                        Dmjpro.

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by r035198x
                          Always use a regular expression for this stuff, something like [CODE=java]Pattern.compile (".+@.+\\.[a-z]+");[/CODE]
                          That thing accepts two or more dots next to each other and multiple at-signs
                          all over the place. I'm afraid the RE is more complicated than that:

                          1) [^\.@]+

                          accepts anything without dots and at-signs, so

                          2) [^\.@]+(\.[\.@]+)*

                          accepts sequences of 1) separated by a single dot. This little beast accepts
                          all stuff to the left of the at-sign in a (potential) email address.

                          The part to the right of the at-sign is similar to 2) but there's a top level domain
                          name as the rightmost part:

                          3) [^\.@]+(\.[\.@]+)*\\.[a-z]+

                          Now 2) and 3) together, separated by an at-sign accepts an email address:

                          [^\.@]+(\.[\.@]+)*@[^\.@]+(\.[\.@]+)*\\.[a-z]+

                          *yuck* ;-)

                          kind regards,

                          Jos

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by JosAH
                            That thing accepts two or more dots next to each other and multiple at-signs
                            all over the place. I'm afraid the RE is more complicated than that:

                            1) [^\.@]+

                            accepts anything without dots and at-signs, so

                            2) [^\.@]+(\.[\.@]+)*

                            accepts sequences of 1) separated by a single dot. This little beast accepts
                            all stuff to the left of the at-sign in a (potential) email address.

                            The part to the right of the at-sign is similar to 2) but there's a top level domain
                            name as the rightmost part:

                            3) [^\.@]+(\.[\.@]+)*\\.[a-z]+

                            Now 2) and 3) together, separated by an at-sign accepts an email address:

                            [^\.@]+(\.[\.@]+)*@[^\.@]+(\.[\.@]+)*\\.[a-z]+

                            *yuck* ;-)

                            kind regards,

                            Jos
                            I just plugged that from devx and never bothered to check it.

                            There's another way of doing it in parts doesn't look cleaner as well (but regexs are not always clean anyway).

                            So you did a match by eliminating the illegal characters, the OP doesn't want to use them anyway ...

                            Comment

                            • JosAH
                              Recognized Expert MVP
                              • Mar 2007
                              • 11453

                              #15
                              Originally posted by r035198x
                              I just plugged that from devx and never bothered to check it.

                              There's another way of doing it in parts doesn't look cleaner as well (but regexs are not always clean anyway).

                              So you did a match by eliminating the illegal characters, the OP doesn't want to use them anyway ...
                              Ah, yes, if you assume that the email address is correct anyway, your RE:
                              ".+@.+\\.[a-z]+" works fine of course. There's an old saying in the compiler
                              'world' that goes: "any fool can write a compiler for a perfect user" (no insult
                              intended).

                              kind regards,

                              Jos@.....@@.@.@ .@....foo

                              Comment

                              Working...