473,502 Members | 11,025 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Code for validating email address in C# - validate email

Here's a small method for validating email in C#. It may save you some time..

public static bool IsValidEmailAddress(string sEmail)
{
if (sEmail == null)
{
return false;
}

int nFirstAT = sEmail.IndexOf('@');
int nLastAT = sEmail.LastIndexOf('@');

if ( (nFirstAT > 0) && (nLastAT == nFirstAT) &&
(nFirstAT < (sEmail.Length - 1)) )
{
// address is ok regarding the single @ sign
return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
}
else
{
return false;
}
}
Nov 15 '05 #1
1 71019
he***********@northnode.se (Henrik Nyberg) wrote in
news:7d**************************@posting.google.c om:
Here's a small method for validating email in C#. It may save
you some time..

public static bool IsValidEmailAddress(string sEmail)
{
if (sEmail == null)
{
return false;
}

int nFirstAT = sEmail.IndexOf('@');
int nLastAT = sEmail.LastIndexOf('@');

if ( (nFirstAT > 0) && (nLastAT == nFirstAT) &&
(nFirstAT < (sEmail.Length - 1)) )
{
// address is ok regarding the single @ sign
return (Regex.IsMatch(sEmail, @"(\w+)@(\w+)\.(\w+)"));
}
else
{
return false;
}
}


Henrik,

You could shorten your code by removing the check for the @ symbol.
It's redundant since the regex will fail if the supplied email
address has more than one @ symbol.

Also, your regex is too restrictive. It will return false for a
valid email address like go******@governor.ca.gov.

Try this:

public static bool IsValidEmailAddress(string sEmail)
{
if (sEmail == null)
{
return false;
}
else
{
return Regex.IsMatch(sEmail, @"
^
[-a-zA-Z0-9][-.a-zA-Z0-9]*
@
[-.a-zA-Z0-9]+
(\.[-.a-zA-Z0-9]+)*
\.
(
com|edu|info|gov|int|mil|net|org|biz|
name|museum|coop|aero|pro
|
[a-zA-Z]{2}
)
$",
RegexOptions.IgnorePatternWhitespace);
}
}
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
6196
by: AnnMarie | last post by:
<script language="JavaScript" type="text/javascript"> <!-- function validate(theForm) { var validity = true; // assume valid if(frmComments.name.value=='' && validity == true) { alert('Your...
17
4882
by: Sue | last post by:
<html> Is there someone here that can help me validate the period as the fourth from the last character in an email address. There is other information and validation on the form I have to do but...
7
3569
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
15
20805
by: qazmlp | last post by:
What is the best & fastest way of validating an IPv4 address? Basically, the input can be either in IPAddressv4 or IPAddressv4:port format. Currently I have the following code to validate the...
2
6607
by: Doug | last post by:
I'm a little confused by this functionality. It doesn't seem to be behaving like it should. I am using the following regular expression to validate email addresses:...
6
1595
by: yochessyo | last post by:
Hi, I would like to validate email addresses. I am not interested to validate it with a regex expression but from the email server where the addresses are. I would like to query this server and...
3
2382
by: Tom Anderson | last post by:
Hi all, A hoary old chestnut this - any advice on how to syntactically validate an email address? I'd like to support both the display-name-and-angle-bracket and bare-address forms, and to allow...
26
25560
by: webrod | last post by:
Hi, I have some php pages with a lot of HTML code. I am looking for a HTML validator tool (like TIDY). TIDY is not good enough with PHP tags (it removes a lot of php code). Do you have any...
0
7059
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7249
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6960
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7437
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5548
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4981
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1481
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
716
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.