Validate email

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AishaKhalfan
    New Member
    • Apr 2007
    • 23

    Validate email

    Hi all,

    I have to send an email in VB.Net 2005 and successfully it works. But now I have a problem I want to validate the txtformbox that the users should enter a validate email. For example they should put @ symbol.


    So any volunteer to help me???


    I am looking forward to answering my email.


    Here you are the code:


    Private Sub btnSend_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnSend.Click

    'Create mail object'
    Dim mail As New MailMessage()

    'Set Adresses'
    mail.From = New MailAddress(txt From.Text)
    mail.To.Add(txt To.Text)

    'Set the content
    mail.Subject = txtSubject.Text
    mail.Body = txtContent.Text

    'Create Smtp object'
    Dim smtp As New SmtpClient("loc alhost")

    'Send email'
    smtp.Send(mail)
    'Show message that the email has been sent to the person'
    MsgBox("Success fully message has been sent.", MsgBoxStyle.Inf ormation, "Confirmati on Message")
    End Sub



    Regards,

    Aisha
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    I suggest validating the email using client side script. HTH.

    Comment

    • AishaKhalfan
      New Member
      • Apr 2007
      • 23

      #3
      Originally posted by kenobewan
      I suggest validating the email using client side script. HTH.

      Could you explain further about it, because I didn’t get it.


      Regards,
      Aisha

      Comment

      • dscreation
        New Member
        • May 2007
        • 13

        #4
        check out this link
        Build web apps and services that run on Windows, Linux, and macOS using C#, HTML, CSS, and JavaScript. Get started for free on Windows, Linux, or macOS.


        go down to....

        “How Do I?” with ASP.NET

        then watch the video tutorial...
        Create a Contact Us Page.

        he goes over an email validation control that might come in handy.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by AishaKhalfan
          Could you explain further about it, because I didn’t get it.


          Regards,
          Aisha
          Hi Aisha,

          Kenobewan's suggestion of a client side validation is probably the easiest.
          • Go to your tool box, under the validation section and drag a "RegularExpresi onValidator" onto the page.
          • Rename this validator.
          • Set its "ValidationExpr ession" property to "email" (click the little dots to select a prebuilt expression)
          • Set its "ControlToValid ate" to the text box that's meant for the email.


          This will register a JavaScript during run time that will check the textbox to make sure that its a valid email before any submitting will be allowed.

          Cheers!

          -Frinny

          Comment

          • AishaKhalfan
            New Member
            • Apr 2007
            • 23

            #6
            Originally posted by Frinavale
            Hi Aisha,

            Kenobewan's suggestion of a client side validation is probably the easiest.
            • Go to your tool box, under the validation section and drag a "RegularExpresi onValidator" onto the page.
            • Rename this validator.
            • Set its "ValidationExpr ession" property to "email" (click the little dots to select a prebuilt expression)
            • Set its "ControlToValid ate" to the text box that's meant for the email.


            This will register a JavaScript during run time that will check the textbox to make sure that its a valid email before any submitting will be allowed.

            Cheers!

            -Frinny



            Hi,


            This is a great idea for ASP.NET but my application is Windows based, so that one won’t work.


            Thanks a lot


            Regards,
            Aisha

            Comment

            • vanc
              Recognized Expert New Member
              • Mar 2007
              • 211

              #7
              An quick idea about this issue is split information in address and check it.

              For a single email address like "abc@aaa.com.au "

              1. you split this string by "@" and check for its length--->the length must be two
              2. if the length is two, check for the first part is "abc" for any incompatible characters by using Contains method, you have to get an array of all incompatible characers, they are not many actually.
              3. repeat the step 2 with the second part of the address and split the other part of email, here is "aaa.com.au ", split it with "." and check for length, the length should between 2 to 3

              Cheers.

              Comment

              • AishaKhalfan
                New Member
                • Apr 2007
                • 23

                #8
                Originally posted by vanc
                An quick idea about this issue is split information in address and check it.

                For a single email address like "abc@aaa.com.au "

                1. you split this string by "@" and check for its length--->the length must be two
                2. if the length is two, check for the first part is "abc" for any incompatible characters by using Contains method, you have to get an array of all incompatible characers, they are not many actually.
                3. repeat the step 2 with the second part of the address and split the other part of email, here is "aaa.com.au ", split it with "." and check for length, the length should between 2 to 3

                Cheers.


                HI Cheers,

                This is what I done, Please advice me if there is any error.

                However, I don't know how to do the second step, help me please.


                'Create mail object'
                Dim mail As New MailMessage()

                'Set Adresses'
                'make sure to include symbol @ in textbox From & text box To
                If txtFrom.Text.Co ntains("@") And txtTo.Text.Cont ains("@") And txtFrom.Text.Le ngth > 2 And txtTo.Text.Leng th > 2 Then
                mail.From = New MailAddress(txt From.Text)
                mail.To.Add(txt To.Text)

                'Set the content
                mail.Subject = txtSubject.Text
                mail.Body = txtContent.Text

                'Create Smtp object'
                Dim smtp As New SmtpClient("loc alhost")

                'Send email'
                smtp.Send(mail)
                'Show message that the email has been sent to the person'
                MsgBox("Success fully message has been sent.", MsgBoxStyle.Inf ormation, "Confirmati on Message")
                Else
                MsgBox("Please Enter valid email")
                End If


                Regards,

                Aisha

                Comment

                • vanc
                  Recognized Expert New Member
                  • Mar 2007
                  • 211

                  #9
                  1. Spit the address string
                  eg: "abc@aaa.com.au "
                  eAddress = txtFrom.Text;
                  string[] address = eAddress.Split( '@');
                  if(address.Leng th != 2) return false;
                  if(address.Leng th == 2)
                  {
                  foreach(char aChar in compatibleChars ) //compatibleChars is an array that contains all compatible character for email address like '.' '-' '_' etc...
                  {

                  }
                  }

                  Comment

                  • vanc
                    Recognized Expert New Member
                    • Mar 2007
                    • 211

                    #10
                    Sorry I hit Submit button....:D

                    1. Spit the address string
                    eg: "abc@aaa.com.au "
                    eAddress = txtFrom.Text;
                    string[] address = eAddress.Split( '@');
                    if(address.Leng th != 2) return false;
                    if(address.Leng th == 2)
                    {
                    foreach(char aChar in compatibleChars ) //compatibleChars is an array that contains all compatible character for email address like '.' '-' '_' etc...
                    {
                    if(!address[0].Contains(aChar )) return false;
                    if(!address[1].Contains(aChar )) return false;
                    }
                    //at this far both side of the address is ok but it needs to be checked for valid //domain. So just split it again with '.'

                    string[] domain = address[1].Split('.');
                    if(domain.Lengt h < 2) return false;
                    if(domain.Lengt h > 3) return false;
                    //domain of address should be something like .com or .com.au if you know any
                    //different type of domain, just add it in.
                    }

                    hope this help

                    cheers.

                    Comment

                    • AishaKhalfan
                      New Member
                      • Apr 2007
                      • 23

                      #11
                      Originally posted by vanc
                      Sorry I hit Submit button....:D

                      1. Spit the address string
                      eg: "abc@aaa.com.au "
                      eAddress = txtFrom.Text;
                      string[] address = eAddress.Split( '@');
                      if(address.Leng th != 2) return false;
                      if(address.Leng th == 2)
                      {
                      foreach(char aChar in compatibleChars ) //compatibleChars is an array that contains all compatible character for email address like '.' '-' '_' etc...
                      {
                      if(!address[0].Contains(aChar )) return false;
                      if(!address[1].Contains(aChar )) return false;
                      }
                      //at this far both side of the address is ok but it needs to be checked for valid //domain. So just split it again with '.'

                      string[] domain = address[1].Split('.');
                      if(domain.Lengt h < 2) return false;
                      if(domain.Lengt h > 3) return false;
                      //domain of address should be something like .com or .com.au if you know any
                      //different type of domain, just add it in.
                      }

                      hope this help

                      cheers.

                      Thanks a lot cheers for supporting. Good bless you


                      Regards,

                      Aisha

                      Comment

                      Working...