claracruz
asked on
Check if string contains @
Hello Experts,
How do I check if a string is an email address or not
Thanks
How do I check if a string is an email address or not
Thanks
u can use regularexpression validator, or use System.Text.RegularExpress ion
example of regularexpression validator from http://forums.asp.net/thread/1054492.aspx
<p>
<asp:TextBox id="TextBox1" runat="server">
</asp:TextBox>
<asp:Button id="Button1" runat="server"
Text="Click here">
</asp:Button>
</p>
<p>
<asp:RegularExpressionVali dator
id="RegularExpressionValid ator1"
runat="server" ValidationExpression="\w+( [-+.]\w+)* @\w+([-.]\ w+)*\.\w+( [-.]\w+)*"
ErrorMessage="Please enter a valid E-mail ID"
ControlToValidate="TextBox 1">
</asp:RegularExpressionVal idator>
</p>
example of regularexpression validator from http://forums.asp.net/thread/1054492.aspx
<p>
<asp:TextBox id="TextBox1" runat="server">
</asp:TextBox>
<asp:Button id="Button1" runat="server"
Text="Click here">
</asp:Button>
</p>
<p>
<asp:RegularExpressionVali
id="RegularExpressionValid
runat="server" ValidationExpression="\w+(
ErrorMessage="Please enter a valid E-mail ID"
ControlToValidate="TextBox
</asp:RegularExpressionVal
</p>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi,
function isValidEmail(emailaddr)
{
var filter=/^([\w-]+(?:\.[\w-] +)*)@((?:[ \w-]+\.)*\ w[\w-]{0,6 6})\.([a-z ]{2,6}(?:\ .[a-z]{2}) ?)$/i
if (filter.test(emailaddr))
{
return true;
}
else
{
return false;
}
}
Can use this function also it will return whether the emaila ddress i s valid or not
Nanda
function isValidEmail(emailaddr)
{
var filter=/^([\w-]+(?:\.[\w-]
if (filter.test(emailaddr))
{
return true;
}
else
{
return false;
}
}
Can use this function also it will return whether the emaila ddress i s valid or not
Nanda
ASKER
Thanx guys,
hOW WOULD i DO THIS BIT IN C#
If MyString.IndexOf("@") > 0 Then
'It contains @
hOW WOULD i DO THIS BIT IN C#
If MyString.IndexOf("@") > 0 Then
'It contains @
var emailAddress='test@texs... com';
var regexp;
var retval;
regexp=new RegExp(/^[a-zA-Z][\w\.-]*[ a-zA-Z0-9] @[a-zA-Z0- 9][\w\.-]* [a-zA-Z0-9 ]\.[a-zA-Z ][a-zA-Z\. ]*[a-zA-Z] $/);
retval=emailAddress.match( regexp);
if(retval==null)
{
alert("invalid email address ");
}
else
{
alert("valid email address ");
}
in c#
--------------
public void TestEmailRegex()
{
string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\ w+)*\.\w+( [-.]\w+)*" ;
Regex reLenient = new Regex(patternLenient);
string patternStrict = @"^(([^<>()[\]\\.,;:\s@\"" ]+"
+ @"(\.[^<>()[\]\\.,;:\s@\"" ]+)*)|(\"" .+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1, 3}\.[0-9]{ 1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA- Z\-0-9]+\. )+"
+ @"[a-zA-Z]{2,}))$";
Regex reStrict = new Regex(patternStrict);
ArrayList samples = new ArrayList();
samples.Add("joe");
samples.Add("joe@home");
samples.Add("a@b.c");
samples.Add("joe@home.com" );
samples.Add("joe.bob@home. com");
samples.Add("joe-bob[at]ho me.com");
samples.Add("joe@his.home. com");
samples.Add("joe@his.home. place");
samples.Add("joe@home.org" );
samples.Add("joe@joebob.na me");
samples.Add("joe.@bob.com" );
samples.Add(".joe@bob.com" );
samples.Add("joe<>bob@bob. come");
samples.Add("joe&bob@bob.c om");
samples.Add("~joe@bob.com" );
samples.Add("joe$@bob.com" );
samples.Add("joe+bob@bob.c om");
samples.Add("o'reilly@ther e.com");
string output = "<table border=1>";
output += "<tr><td><b>Email</b></td> <td><b>Pat tern</b>"
+ "</td><td><b>Valid Email?</b></td></tr>";
bool toggle = true;
foreach (string sample in samples)
{
string bgcol = "white";
if (toggle)
bgcol = "gainsboro";
toggle = !toggle;
bool isLenientMatch = reLenient.IsMatch(sample);
if (isLenientMatch)
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Lenient</td><td> Is Valid</td></tr>";
else
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Lenient</td><td> Is NOT Valid</td></tr>";
bool isStrictMatch = reStrict.IsMatch(sample);
if (isStrictMatch)
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Strict</td><td>I s Valid</td></tr>";
else
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Strict</td><td>I s NOT Valid</td></tr>";
}
output += "</table>";
lblOutput.Text = output;
}
var regexp;
var retval;
regexp=new RegExp(/^[a-zA-Z][\w\.-]*[
retval=emailAddress.match(
if(retval==null)
{
alert("invalid email address ");
}
else
{
alert("valid email address ");
}
in c#
--------------
public void TestEmailRegex()
{
string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\
Regex reLenient = new Regex(patternLenient);
string patternStrict = @"^(([^<>()[\]\\.,;:\s@\""
+ @"(\.[^<>()[\]\\.,;:\s@\""
+ @"((\[[0-9]{1,3}\.[0-9]{1,
+ @"\.[0-9]{1,3}\])|(([a-zA-
+ @"[a-zA-Z]{2,}))$";
Regex reStrict = new Regex(patternStrict);
ArrayList samples = new ArrayList();
samples.Add("joe");
samples.Add("joe@home");
samples.Add("a@b.c");
samples.Add("joe@home.com"
samples.Add("joe.bob@home.
samples.Add("joe-bob[at]ho
samples.Add("joe@his.home.
samples.Add("joe@his.home.
samples.Add("joe@home.org"
samples.Add("joe@joebob.na
samples.Add("joe.@bob.com"
samples.Add(".joe@bob.com"
samples.Add("joe<>bob@bob.
samples.Add("joe&bob@bob.c
samples.Add("~joe@bob.com"
samples.Add("joe$@bob.com"
samples.Add("joe+bob@bob.c
samples.Add("o'reilly@ther
string output = "<table border=1>";
output += "<tr><td><b>Email</b></td>
+ "</td><td><b>Valid Email?</b></td></tr>";
bool toggle = true;
foreach (string sample in samples)
{
string bgcol = "white";
if (toggle)
bgcol = "gainsboro";
toggle = !toggle;
bool isLenientMatch = reLenient.IsMatch(sample);
if (isLenientMatch)
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Lenient</td><td>
else
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Lenient</td><td>
bool isStrictMatch = reStrict.IsMatch(sample);
if (isStrictMatch)
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Strict</td><td>I
else
output += "<tr bgcolor=" + bgcol + "><td>"
+ sample + "</td><td>Strict</td><td>I
}
output += "</table>";
lblOutput.Text = output;
}
Hi,
Include the name space "System.Text.RegularExpres sions" in ur page
Private Sub MyValidatingCode()
' Confirm there is text in the control.
If TextBox1.Text.Length = 0 Then
Throw New Exception("Email address is a required field")
Else
If Not Regex.IsMatch(TextBox1.Tex t, "\w+([-+.]\w+)*@\w+([-.]\w +)*\.\w+([ -.]\w+)*") Then
MsgBox("Invalid emailid")
End If
End If
End Sub
The above function will validate for the email id
Thanks,
NAnda
Include the name space "System.Text.RegularExpres
Private Sub MyValidatingCode()
' Confirm there is text in the control.
If TextBox1.Text.Length = 0 Then
Throw New Exception("Email address is a required field")
Else
If Not Regex.IsMatch(TextBox1.Tex
MsgBox("Invalid emailid")
End If
End If
End Sub
The above function will validate for the email id
Thanks,
NAnda
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
function isValidEmailId(elm)
{
var id=document.getElementById
var string = id.value;
if (string.search(/^\w+((-\w+
{
return true;
}
else
{
alert("Email address is not valid ! ");
id.focus();
id.select();
return false;
}
}
hope it helps u :)]thanx