Link to home
Start Free TrialLog in
Avatar of claracruz
claracruz

asked on

Check if string contains @

Hello Experts,

How do I check if a string is an email address or not

Thanks
Avatar of Pratima
Pratima
Flag of India image

use this javascript function

 function isValidEmailId(elm)
   {
       var id=document.getElementById(elm);
       var string = id.value;
            if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
            {
                  return true;
            }
            else
            {
                  alert("Email address is not valid ! ");
                  id.focus();
                  id.select();
                  return false;
            }
   }

hope it helps u :)]thanx
u can use regularexpression validator,  or use System.Text.RegularExpression
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:RegularExpressionValidator
            id="RegularExpressionValidator1"
            runat="server" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
            ErrorMessage="Please enter a valid E-mail ID"
            ControlToValidate="TextBox1">
            </asp:RegularExpressionValidator>
        </p>
SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,
   function isValidEmail(emailaddr)
            {
                  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([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
Avatar of claracruz
claracruz

ASKER

Thanx guys,


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]home.com");
   samples.Add("joe@his.home.com");
   samples.Add("joe@his.home.place");
   samples.Add("joe@home.org");
   samples.Add("joe@joebob.name");
   samples.Add("joe.@bob.com");
   samples.Add(".joe@bob.com");
   samples.Add("joe<>bob@bob.come");
   samples.Add("joe&bob@bob.com");
   samples.Add("~joe@bob.com");
   samples.Add("joe$@bob.com");
   samples.Add("joe+bob@bob.com");
   samples.Add("o'reilly@there.com");

   string output = "<table border=1>";
   output += "<tr><td><b>Email</b></td><td><b>Pattern</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>Is Valid</td></tr>";
      else
         output += "<tr bgcolor=" + bgcol + "><td>"
            + sample + "</td><td>Strict</td><td>Is NOT Valid</td></tr>";

   }
   output += "</table>";

   lblOutput.Text = output;

}

Hi,
    Include the name space "System.Text.RegularExpressions" 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.Text, "\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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial