Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Validate emailaddress

Validate emailaddress

Scheduled Pinned Locked Moved C#
csharp
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hitesh kalra
    wrote on last edited by
    #1

    How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance

    P M OriginalGriffO 3 Replies Last reply
    0
    • H hitesh kalra

      How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance

      M Offline
      M Offline
      MumbleB
      wrote on last edited by
      #2

      The best way of doing this is using Regex. Try the below code.

          private void txtboxEmailAdd\_Validating(object sender, CancelEventArgs e)
          {
              string pattern = @"^\[a-z\]\[a-z|0-9|\]\*(\[\_\]\[a-z|0-9\]+)\*
      

      ([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
      System.Text.RegularExpressions.Match match =
      Regex.Match(txtboxEmailAdd.Text.Trim(), pattern, RegexOptions.IgnoreCase);
      if (match.Success || txtboxEmailAdd.Text == "")
      return;
      else
      MessageBox.Show("Invalid Email Address" + "\r\n" + "Please enter Valid Email", "Invalid Email Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
      e.Cancel = false;
      }

      Excellence is doing ordinary things extraordinarily well.

      1 Reply Last reply
      0
      • H hitesh kalra

        How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Use a regular expression. There are several examples of email validation on google - some more accurate than others, so take your time and read around the various implementations (i.e. look for ones that can handle more complex email addresses such as .museum addresses).

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        1 Reply Last reply
        0
        • H hitesh kalra

          How will I validate the text of email address. I am making a window application in c# .net, in the text box I want that the textbox of email contains the '@' without written @ it gives a messagebox that it is not valid email address. thanx in advance

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Didn't read the question properly!

          string s = "Hello@email.com";
          if (s.IndexOf("@") <= 0)
          {
          MessageBox.Show("Not an EMAIL address");
          }

          This will report on @email.com and email.com, but not on a@email.com

          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          P 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Didn't read the question properly!

            string s = "Hello@email.com";
            if (s.IndexOf("@") <= 0)
            {
            MessageBox.Show("Not an EMAIL address");
            }

            This will report on @email.com and email.com, but not on a@email.com

            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            This is not a workable solution. What happens if the user enters @me, or bob@bob, or ¬¬¬@¬¬¬?

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            OriginalGriffO 1 Reply Last reply
            0
            • P Pete OHanlon

              This is not a workable solution. What happens if the user enters @me, or bob@bob, or ¬¬¬@¬¬¬?

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Agreed it is not the complete solution - it covers the @me case since the indexof returns 0 - but I didn't want to confuse him too much, just answer the question as asked. :) A regular expression would work better, but I guess the "proper" solution would be to interface to Outlook (or express, or whatever) and check the entered address against his contacts list - ouch!

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              M 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                Agreed it is not the complete solution - it covers the @me case since the indexof returns 0 - but I didn't want to confuse him too much, just answer the question as asked. :) A regular expression would work better, but I guess the "proper" solution would be to interface to Outlook (or express, or whatever) and check the entered address against his contacts list - ouch!

                No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                M Offline
                M Offline
                MumbleB
                wrote on last edited by
                #7

                Have a look at the RegEx Expression I gave him. It caters for all the validation, checks on the domain etc etc and that there is a valid name@something.com etc. I think I added a check in that there are no spaces in the email address either. I think it is pretty comprehensive in what it checks. Even if I have to say so myself.... ;)

                Excellence is doing ordinary things extraordinarily well.

                M P 2 Replies Last reply
                0
                • M MumbleB

                  Have a look at the RegEx Expression I gave him. It caters for all the validation, checks on the domain etc etc and that there is a valid name@something.com etc. I think I added a check in that there are no spaces in the email address either. I think it is pretty comprehensive in what it checks. Even if I have to say so myself.... ;)

                  Excellence is doing ordinary things extraordinarily well.

                  M Offline
                  M Offline
                  Mycroft Holmes
                  wrote on last edited by
                  #8

                  Kwagga wrote:

                  Even if I have to say so myself....

                  Smartass :-D - your code earns you a perma link as I'm certain to need this sometime in the future! Thanks

                  Never underestimate the power of human stupidity RAH

                  M 1 Reply Last reply
                  0
                  • M Mycroft Holmes

                    Kwagga wrote:

                    Even if I have to say so myself....

                    Smartass :-D - your code earns you a perma link as I'm certain to need this sometime in the future! Thanks

                    Never underestimate the power of human stupidity RAH

                    M Offline
                    M Offline
                    MumbleB
                    wrote on last edited by
                    #9

                    NO worries mate. Glad I could help 1 person today.....

                    Excellence is doing ordinary things extraordinarily well.

                    1 Reply Last reply
                    0
                    • M MumbleB

                      Have a look at the RegEx Expression I gave him. It caters for all the validation, checks on the domain etc etc and that there is a valid name@something.com etc. I think I added a check in that there are no spaces in the email address either. I think it is pretty comprehensive in what it checks. Even if I have to say so myself.... ;)

                      Excellence is doing ordinary things extraordinarily well.

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      The original RFC regex is (as follows):

                      (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

                      It's not perfect, but is a starting point.

                      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                      My blog | My articles | MoXAML PowerToys | Onyx

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups