Hi,

i'm making a regitration form. In that i need to validate the email taken from user. for thgat i've written a class in .cs file

public bool ValidateEmailAddress(String txt)
    {
        // Confirm there is text in the control.
        bool abc = false;
        if (txt.Trim().Length != 0)
        {
            //	Confirm that there is a "." and an "@" in the e-mail address.
            if (txt.IndexOf(".") == -1 || txt.IndexOf("@") == -1)
            {
                abc= false;
            }
            else
            {
                abc= true;
            }

        }
        return abc;         
    }

Now i need to implement the method whle taking the data from user. can anyone pls guide

Thanks

SqlCommand cmd_insertuserDetail = new SqlCommand ("Insert into fwm_user (last_name, first_name, birth_date, address, city, postal_code, country, email, telephone, occupation, company)" +
                                       " values (@last_name, @first_name, @birth_date, @address, @city, @postal_code, @country, @email, @telephone, @occupation, @company)", conn1);
      
            cmd_insertuserDetail.Parameters.Add(new SqlParameter("@last_name", txtLName.Text));
            .......
            //cmd_insertuserDetail.Parameters.Add(new SqlParameter("@email", txtEmail.Text));
            if (ValidateEmailAddress(txtEmail.Text)) ????????????
            {
                cmd_insertuserDetail.Parameters.Add(new SqlParameter("@email", txtEmail.Text));
            }
            else
            {
                MessageBox.Show("Enter valid Email ID", "Error!!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
            cmd_insertuserDetail.Parameters.Add(new SqlParameter("@telephone", txtTelephone.Text));
           .....

Recommended Answers

All 2 Replies

Hi,
You have written a function for e-mail validation in a seperate cs file.
You can call the function in the onclick event handler as

for suppose your event handler is btnclk then

protected void btnclk(object s,eventargument e)
    {
         // create object of cs class
        bool x=object.ValidateEmailAddress(txtEmail.Text)
        if(x==yes)
         {
           cmd_insertuserDetail.Parameters.Add(new SqlParameter("@email", txtEmail.Text));
          }
        else
        {
           MessageBox.Show("Enter valid Email ID", "Error!!",      MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
         }

     }

however i think u dont need this hectic code when you can use validation controls unless u have some good reason.

thank you.

public static bool IsValidEmailAddr(string email)
    {
      try
      {
        System.Net.Mail.MailAddress addr = new System.Net.Mail.MailAddress(email);
      }
      catch
      {
        return false;
      }

      return true;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.