C# .NET - C# - Asked By Ramendra Kumar on 15-Jan-10 12:30 AM

hi i want to check a valid email in asp.net, using c#, i can't use regular expression or javascript validator

re - Web Star replied to Ramendra Kumar on 15-Jan-10 12:43 AM

Your need is not looks like valid because if u wnat avoid client side validation thats ok , u don't use javascript validation u use server side valudation in C#.net this is significante but u don't wnat to use regular expression this doesn't have any significante because in C#.net u must use expression for validation email as

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);
}

this is alos use regular expression but no need to validation control.

hope this helps u


Validate Email address - mv ark replied to Ramendra Kumar on 15-Jan-10 12:44 AM

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

These may not help in all cases as some non-third party email providers may choose not send a response back when an email is sent to an invalid email address on their domain.

re - Santhosh N replied to Ramendra Kumar on 15-Jan-10 12:52 AM

I am not sure what is stopping from you to use regular expressions or javascript for this as one would feel its the right way to validate the email prior sending or saving whatever the requirement is..

But, if there are alternatives in using validators on serverside

Soemthing like this using regularexpressionvalidator..

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>       
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
            ControlToValidate="TextBox1" ErrorMessage="Please enter correct email address"
            ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">*</asp:RegularExpressionValidator>

There are many other ways as well and you can check this http://forums.asp.net/p/1413372/3106857.aspx for the different ways you can do

re - Ramendra Kumar replied to Web Star on 15-Jan-10 12:56 AM

thanks for reply but frind will u told me what is NulltoString and what is Regex bcz i am getting error

i think i must be use somenamespace for regex

re - Web Star replied to Ramendra Kumar on 15-Jan-10 01:03 AM

u need to refer the namespace which contain regular expression

The regex classes are located in the namespace System.Text.RegularExpressions. To make them available, place Imports System.Text.RegularExpressions at the start of your source code.

ok - Ramendra Kumar replied to Web Star on 15-Jan-10 01:07 AM

i done tat but actually i am getting error in NulltoString(inputEmail);

i am using C#

plz - Ramendra Kumar replied to Web Star on 15-Jan-10 01:23 AM
plz tell me friend why i am getting error in this line

inputEmail = NulltoString(inputEmail);

use this - Kalit Sikka replied to Ramendra Kumar on 15-Jan-10 02:10 AM

public static bool IsValidEmailID(string sEmailID)

{

Regex oEmail =

new Regex(@"^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$");

return oEmail.IsMatch(sEmailID);

}

Refer: http://www.eggheadcafe.com/tutorials/aspnet/fa89fc70-a4a2-4ef6-8cea-2010d4cc6138/regular-expression-in-ne.aspx


re - Web Star replied to Ramendra Kumar on 15-Jan-10 02:21 AM

hi

NulltoString is just remove all space and check fot null so either u can wrtie a method for that or remove it other than also work perfectly


 

frnd replied to Ramendra Kumar on 30-Sep-11 08:27 AM
u urself are a bug...