ASP.NET - Validation For Email in Asp.net - Asked By srujan on 08-Sep-10 07:42 AM

Hi Friends,
        how to validate an email address in asp.net......
Thanks & Regards
Sujju
R132 G132 replied to srujan on 08-Sep-10 07:45 AM
U can use 

RegularExpressionValidator

control Validationexpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
Anand Malli replied to srujan on 08-Sep-10 07:46 AM
Hi Srujan,

In asp.net you can use regular expression validator to validate your email like following

<asp:TextBox ID="txtEmail" runat="server" Width="242px"></asp:TextBox>
    <asp:RegularExpressionValidator ID="validateEmail" runat="server" Display="Dynamic"
      ControlToValidate="txtEmail" ErrorMessage="Invalid Email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
      ValidationGroup="validateInputs"></asp:RegularExpressionValidator>

i think its easy...

Thanks
Kirtan Patel replied to srujan on 08-Sep-10 07:57 AM
you can do it like below using RegularExpression Validator
 

Snippet
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" 
      ControlToValidate="TextBox1" Display="Dynamic" ErrorMessage=" Email Invalid" 
      ForeColor="Maroon" SetFocusOnError="True" 
      ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator>
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
  




Sagar P replied to srujan on 08-Sep-10 09:02 AM
Use this function to check email is valid or not;

public
static bool isEmail(string inputEmail)
{
   inputEmail  = NulltoString(inputEmail);
   string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
     @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
     @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
   Regex re = new Regex(strRegex);
   if (re.IsMatch(inputEmail))
    return (true);
   else
    return (false);
}

Besides client-side validation that provides a basic check, you can perform DNS &  SMTP validation. You can find the code to do that here -
http://www.coveryourasp.com/ValidateEmail.asp

http://www.codeproject.com/KB/validation/Valid_Email_Addresses.aspx
Super Man replied to srujan on 08-Sep-10 09:26 AM

You can also use this javascript code for validating  your email id :-

 

<script type="text/javascript">

 

function RegexEmail(emailInputBox){   

   var emailStr = document.getElementById(emailInputBox).value; 

   var emailRegexStr = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

   var isvalid = emailRegexStr.test(emailStr);

    if (!isvalid) {

 alert('Invalid email address!');

 emailInputBox.focus;

 }

 }

</script>

<input name="emailInput" id="emailInput" maxlength="60" />

<input id="btn1" type="button" value="Validate Email" onclick="return RegexEmail('emailInput')" />

 

 

Goniey N (Mr. G) replied to srujan on 08-Sep-10 11:48 PM
-- Use Below Code :

01.protected void Button1_Click(object sender, EventArgs e)
02.{
03.  System.Text.RegularExpressions.Regex MyRegex = new System.Text.RegularExpressions.Regex(@"^(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{2})(\.[a-z]{2})*$)/i$");
04.    if (MyRegex.IsMatch(txtdays.Text))
05.  {
06.      Label3.Text = "Valid Email Address.";
07.         
08.  }
09.    else
10.    {
11.      Label3.Text = "InValid Email Address.";
12.  }
13.}


-- For More Clear View See Below Images :


-- It's Valid Email Address.....



-- It's Not a Valid Email Address.....



-- It Will Work 100%.....

-- Hope This Will Help You.....
srujan replied to R132 G132 on 09-Sep-10 01:58 AM
Thank u Friend..