0

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));
           .....
3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by sknake
0

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.

Edited by Ezzaral: Added [code] [/code] tags. Please use them to format all code that you post.

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

      return true;
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.