How to validate textbox not to accept emailids











up vote
1
down vote

favorite












I have one field Company profile: textbox



If the user enters any emailid in textbox,validation errormessage should display that user cant enter emailids in textbox.



i have tried the following code:



Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");

string values = commentstxt.Text.Trim().Split(' ');
for (int i = 0; i < values.Length; i++)
{
bool isValid = regex.IsMatch(values[i].ToString().Trim());
if (isValid)
{
//ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
//break;
Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
break;
}
else
{
Server.Transfer("addlistingpost.aspx", true);
}
}


If the user enters only test@gmail.com,it gives the validation message saying you cannot enter emailid in the textbox which is correct and stays in the addlisting.aspx page.



If the user enters say hello..how are you,it redirects to the addlistingpost.aspx which is also correct.



The issue comes when user enters say hello test@gmail.com how are you,it does not throw a validation message as emailid is present in the textbox.
I know here that it is only comparing values[0] which is hello and then directly goes into the else part.



How to achieve this?



Any help will be highly appreciated.










share|improve this question
























  • What is an "emailids"?
    – Uwe Keim
    Nov 21 at 9:52










  • Also, the regex you're using doesn't match the RFC 5322 spec for legal email addresses.
    – Enigmativity
    Nov 21 at 10:08

















up vote
1
down vote

favorite












I have one field Company profile: textbox



If the user enters any emailid in textbox,validation errormessage should display that user cant enter emailids in textbox.



i have tried the following code:



Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");

string values = commentstxt.Text.Trim().Split(' ');
for (int i = 0; i < values.Length; i++)
{
bool isValid = regex.IsMatch(values[i].ToString().Trim());
if (isValid)
{
//ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
//break;
Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
break;
}
else
{
Server.Transfer("addlistingpost.aspx", true);
}
}


If the user enters only test@gmail.com,it gives the validation message saying you cannot enter emailid in the textbox which is correct and stays in the addlisting.aspx page.



If the user enters say hello..how are you,it redirects to the addlistingpost.aspx which is also correct.



The issue comes when user enters say hello test@gmail.com how are you,it does not throw a validation message as emailid is present in the textbox.
I know here that it is only comparing values[0] which is hello and then directly goes into the else part.



How to achieve this?



Any help will be highly appreciated.










share|improve this question
























  • What is an "emailids"?
    – Uwe Keim
    Nov 21 at 9:52










  • Also, the regex you're using doesn't match the RFC 5322 spec for legal email addresses.
    – Enigmativity
    Nov 21 at 10:08















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have one field Company profile: textbox



If the user enters any emailid in textbox,validation errormessage should display that user cant enter emailids in textbox.



i have tried the following code:



Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");

string values = commentstxt.Text.Trim().Split(' ');
for (int i = 0; i < values.Length; i++)
{
bool isValid = regex.IsMatch(values[i].ToString().Trim());
if (isValid)
{
//ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
//break;
Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
break;
}
else
{
Server.Transfer("addlistingpost.aspx", true);
}
}


If the user enters only test@gmail.com,it gives the validation message saying you cannot enter emailid in the textbox which is correct and stays in the addlisting.aspx page.



If the user enters say hello..how are you,it redirects to the addlistingpost.aspx which is also correct.



The issue comes when user enters say hello test@gmail.com how are you,it does not throw a validation message as emailid is present in the textbox.
I know here that it is only comparing values[0] which is hello and then directly goes into the else part.



How to achieve this?



Any help will be highly appreciated.










share|improve this question















I have one field Company profile: textbox



If the user enters any emailid in textbox,validation errormessage should display that user cant enter emailids in textbox.



i have tried the following code:



Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");

string values = commentstxt.Text.Trim().Split(' ');
for (int i = 0; i < values.Length; i++)
{
bool isValid = regex.IsMatch(values[i].ToString().Trim());
if (isValid)
{
//ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
//break;
Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
break;
}
else
{
Server.Transfer("addlistingpost.aspx", true);
}
}


If the user enters only test@gmail.com,it gives the validation message saying you cannot enter emailid in the textbox which is correct and stays in the addlisting.aspx page.



If the user enters say hello..how are you,it redirects to the addlistingpost.aspx which is also correct.



The issue comes when user enters say hello test@gmail.com how are you,it does not throw a validation message as emailid is present in the textbox.
I know here that it is only comparing values[0] which is hello and then directly goes into the else part.



How to achieve this?



Any help will be highly appreciated.







c# asp.net validation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 9:53









Uwe Keim

27.3k30128210




27.3k30128210










asked Nov 21 at 9:37









chetan kambli

259110




259110












  • What is an "emailids"?
    – Uwe Keim
    Nov 21 at 9:52










  • Also, the regex you're using doesn't match the RFC 5322 spec for legal email addresses.
    – Enigmativity
    Nov 21 at 10:08




















  • What is an "emailids"?
    – Uwe Keim
    Nov 21 at 9:52










  • Also, the regex you're using doesn't match the RFC 5322 spec for legal email addresses.
    – Enigmativity
    Nov 21 at 10:08


















What is an "emailids"?
– Uwe Keim
Nov 21 at 9:52




What is an "emailids"?
– Uwe Keim
Nov 21 at 9:52












Also, the regex you're using doesn't match the RFC 5322 spec for legal email addresses.
– Enigmativity
Nov 21 at 10:08






Also, the regex you're using doesn't match the RFC 5322 spec for legal email addresses.
– Enigmativity
Nov 21 at 10:08














4 Answers
4






active

oldest

votes

















up vote
1
down vote



accepted










You need to scan all the array till any error found. Kind of



Regex regex = new Regex(    @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
string values = commentstxt.Text.Trim().Split(' ');
bool isValid = true; // valid word == not email
for (int i = 0; i < values.Length && isValid; i++)
{
bool isValid = !regex.IsMatch(values[i].ToString().Trim());
if (!isValid)
{
//ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);

Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");

}
}
if (isValid)
{
Server.Transfer("addlistingpost.aspx", true);
}





share|improve this answer





















  • hi @Serg...Is it possible that i can use logical or part in Regex so that it also doesn't accept web address i.e. www.
    – chetan kambli
    Nov 21 at 10:31


















up vote
2
down vote













The Regex that you're using is matching the start (^) and the end ($) of the string.



^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$


Just remove those characters to match anywhere within the line.



([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)


Try this code:



Regex regex = new Regex(@"([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)");
string text = "hello test@gmail.com how are you";
Console.WriteLine(regex.IsMatch(text));


It outputs True.



Here's a regex, by the way, that nearly matches the RFC 5322 spec:



(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])





share|improve this answer






























    up vote
    1
    down vote













    Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
    string values = commentstxt.Text.Trim().Split(' ');

    bool isValid = false;


    for (int i = 0; i < values.Length; i++)
    {

    isValid = regex.IsMatch(values[i].ToString().Trim());


    if (isValid)
    {
    //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
    //break;
    Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
    break;
    }
    else
    {
    continue;

    }


    }

    if(!isValid)
    {
    Server.Transfer("addlistingpost.aspx", true);
    }





    share|improve this answer




























      up vote
      1
      down vote













      Could you try something like this and see if that would work ? :



      Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
      string values = commentstxt.Text.Trim().Split(' ');
      bool hasEmail = false;
      foreach (string str in values)
      {
      bool isCurrentValid = regex.IsMatch(str.Trim());
      if (!isValid)
      {
      hasEmail = false;
      } else {
      hasEmail = true;
      break;
      }
      }

      if(hasEmail) {
      Server.Transfer("addlistingpost.aspx", true);
      }
      else
      {
      Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
      }


      I basically left the logic of checking the whole string out of the loop.






      share|improve this answer





















        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409070%2fhow-to-validate-textbox-not-to-accept-emailids%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote



        accepted










        You need to scan all the array till any error found. Kind of



        Regex regex = new Regex(    @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
        string values = commentstxt.Text.Trim().Split(' ');
        bool isValid = true; // valid word == not email
        for (int i = 0; i < values.Length && isValid; i++)
        {
        bool isValid = !regex.IsMatch(values[i].ToString().Trim());
        if (!isValid)
        {
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);

        Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");

        }
        }
        if (isValid)
        {
        Server.Transfer("addlistingpost.aspx", true);
        }





        share|improve this answer





















        • hi @Serg...Is it possible that i can use logical or part in Regex so that it also doesn't accept web address i.e. www.
          – chetan kambli
          Nov 21 at 10:31















        up vote
        1
        down vote



        accepted










        You need to scan all the array till any error found. Kind of



        Regex regex = new Regex(    @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
        string values = commentstxt.Text.Trim().Split(' ');
        bool isValid = true; // valid word == not email
        for (int i = 0; i < values.Length && isValid; i++)
        {
        bool isValid = !regex.IsMatch(values[i].ToString().Trim());
        if (!isValid)
        {
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);

        Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");

        }
        }
        if (isValid)
        {
        Server.Transfer("addlistingpost.aspx", true);
        }





        share|improve this answer





















        • hi @Serg...Is it possible that i can use logical or part in Regex so that it also doesn't accept web address i.e. www.
          – chetan kambli
          Nov 21 at 10:31













        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You need to scan all the array till any error found. Kind of



        Regex regex = new Regex(    @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
        string values = commentstxt.Text.Trim().Split(' ');
        bool isValid = true; // valid word == not email
        for (int i = 0; i < values.Length && isValid; i++)
        {
        bool isValid = !regex.IsMatch(values[i].ToString().Trim());
        if (!isValid)
        {
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);

        Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");

        }
        }
        if (isValid)
        {
        Server.Transfer("addlistingpost.aspx", true);
        }





        share|improve this answer












        You need to scan all the array till any error found. Kind of



        Regex regex = new Regex(    @"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
        string values = commentstxt.Text.Trim().Split(' ');
        bool isValid = true; // valid word == not email
        for (int i = 0; i < values.Length && isValid; i++)
        {
        bool isValid = !regex.IsMatch(values[i].ToString().Trim());
        if (!isValid)
        {
        //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);

        Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");

        }
        }
        if (isValid)
        {
        Server.Transfer("addlistingpost.aspx", true);
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 9:49









        Serg

        7,85821332




        7,85821332












        • hi @Serg...Is it possible that i can use logical or part in Regex so that it also doesn't accept web address i.e. www.
          – chetan kambli
          Nov 21 at 10:31


















        • hi @Serg...Is it possible that i can use logical or part in Regex so that it also doesn't accept web address i.e. www.
          – chetan kambli
          Nov 21 at 10:31
















        hi @Serg...Is it possible that i can use logical or part in Regex so that it also doesn't accept web address i.e. www.
        – chetan kambli
        Nov 21 at 10:31




        hi @Serg...Is it possible that i can use logical or part in Regex so that it also doesn't accept web address i.e. www.
        – chetan kambli
        Nov 21 at 10:31












        up vote
        2
        down vote













        The Regex that you're using is matching the start (^) and the end ($) of the string.



        ^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$


        Just remove those characters to match anywhere within the line.



        ([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)


        Try this code:



        Regex regex = new Regex(@"([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)");
        string text = "hello test@gmail.com how are you";
        Console.WriteLine(regex.IsMatch(text));


        It outputs True.



        Here's a regex, by the way, that nearly matches the RFC 5322 spec:



        (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])





        share|improve this answer



























          up vote
          2
          down vote













          The Regex that you're using is matching the start (^) and the end ($) of the string.



          ^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$


          Just remove those characters to match anywhere within the line.



          ([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)


          Try this code:



          Regex regex = new Regex(@"([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)");
          string text = "hello test@gmail.com how are you";
          Console.WriteLine(regex.IsMatch(text));


          It outputs True.



          Here's a regex, by the way, that nearly matches the RFC 5322 spec:



          (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])





          share|improve this answer

























            up vote
            2
            down vote










            up vote
            2
            down vote









            The Regex that you're using is matching the start (^) and the end ($) of the string.



            ^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$


            Just remove those characters to match anywhere within the line.



            ([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)


            Try this code:



            Regex regex = new Regex(@"([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)");
            string text = "hello test@gmail.com how are you";
            Console.WriteLine(regex.IsMatch(text));


            It outputs True.



            Here's a regex, by the way, that nearly matches the RFC 5322 spec:



            (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])





            share|improve this answer














            The Regex that you're using is matching the start (^) and the end ($) of the string.



            ^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$


            Just remove those characters to match anywhere within the line.



            ([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)


            Try this code:



            Regex regex = new Regex(@"([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)");
            string text = "hello test@gmail.com how are you";
            Console.WriteLine(regex.IsMatch(text));


            It outputs True.



            Here's a regex, by the way, that nearly matches the RFC 5322 spec:



            (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|\[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|\[x01-x09x0bx0cx0e-x7f])+)])






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 21 at 10:09

























            answered Nov 21 at 10:04









            Enigmativity

            74.6k764129




            74.6k764129






















                up vote
                1
                down vote













                Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                string values = commentstxt.Text.Trim().Split(' ');

                bool isValid = false;


                for (int i = 0; i < values.Length; i++)
                {

                isValid = regex.IsMatch(values[i].ToString().Trim());


                if (isValid)
                {
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
                //break;
                Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                break;
                }
                else
                {
                continue;

                }


                }

                if(!isValid)
                {
                Server.Transfer("addlistingpost.aspx", true);
                }





                share|improve this answer

























                  up vote
                  1
                  down vote













                  Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                  string values = commentstxt.Text.Trim().Split(' ');

                  bool isValid = false;


                  for (int i = 0; i < values.Length; i++)
                  {

                  isValid = regex.IsMatch(values[i].ToString().Trim());


                  if (isValid)
                  {
                  //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
                  //break;
                  Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                  break;
                  }
                  else
                  {
                  continue;

                  }


                  }

                  if(!isValid)
                  {
                  Server.Transfer("addlistingpost.aspx", true);
                  }





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                    string values = commentstxt.Text.Trim().Split(' ');

                    bool isValid = false;


                    for (int i = 0; i < values.Length; i++)
                    {

                    isValid = regex.IsMatch(values[i].ToString().Trim());


                    if (isValid)
                    {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
                    //break;
                    Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                    break;
                    }
                    else
                    {
                    continue;

                    }


                    }

                    if(!isValid)
                    {
                    Server.Transfer("addlistingpost.aspx", true);
                    }





                    share|improve this answer












                    Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                    string values = commentstxt.Text.Trim().Split(' ');

                    bool isValid = false;


                    for (int i = 0; i < values.Length; i++)
                    {

                    isValid = regex.IsMatch(values[i].ToString().Trim());


                    if (isValid)
                    {
                    //ScriptManager.RegisterStartupScript(this, this.GetType(), "CropImage", "alert('you can not enter email id.');", true);
                    //break;
                    Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                    break;
                    }
                    else
                    {
                    continue;

                    }


                    }

                    if(!isValid)
                    {
                    Server.Transfer("addlistingpost.aspx", true);
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 9:51









                    Sagar Shirke

                    581721




                    581721






















                        up vote
                        1
                        down vote













                        Could you try something like this and see if that would work ? :



                        Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                        string values = commentstxt.Text.Trim().Split(' ');
                        bool hasEmail = false;
                        foreach (string str in values)
                        {
                        bool isCurrentValid = regex.IsMatch(str.Trim());
                        if (!isValid)
                        {
                        hasEmail = false;
                        } else {
                        hasEmail = true;
                        break;
                        }
                        }

                        if(hasEmail) {
                        Server.Transfer("addlistingpost.aspx", true);
                        }
                        else
                        {
                        Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                        }


                        I basically left the logic of checking the whole string out of the loop.






                        share|improve this answer

























                          up vote
                          1
                          down vote













                          Could you try something like this and see if that would work ? :



                          Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                          string values = commentstxt.Text.Trim().Split(' ');
                          bool hasEmail = false;
                          foreach (string str in values)
                          {
                          bool isCurrentValid = regex.IsMatch(str.Trim());
                          if (!isValid)
                          {
                          hasEmail = false;
                          } else {
                          hasEmail = true;
                          break;
                          }
                          }

                          if(hasEmail) {
                          Server.Transfer("addlistingpost.aspx", true);
                          }
                          else
                          {
                          Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                          }


                          I basically left the logic of checking the whole string out of the loop.






                          share|improve this answer























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            Could you try something like this and see if that would work ? :



                            Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                            string values = commentstxt.Text.Trim().Split(' ');
                            bool hasEmail = false;
                            foreach (string str in values)
                            {
                            bool isCurrentValid = regex.IsMatch(str.Trim());
                            if (!isValid)
                            {
                            hasEmail = false;
                            } else {
                            hasEmail = true;
                            break;
                            }
                            }

                            if(hasEmail) {
                            Server.Transfer("addlistingpost.aspx", true);
                            }
                            else
                            {
                            Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                            }


                            I basically left the logic of checking the whole string out of the loop.






                            share|improve this answer












                            Could you try something like this and see if that would work ? :



                            Regex regex = new Regex(@"^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");
                            string values = commentstxt.Text.Trim().Split(' ');
                            bool hasEmail = false;
                            foreach (string str in values)
                            {
                            bool isCurrentValid = regex.IsMatch(str.Trim());
                            if (!isValid)
                            {
                            hasEmail = false;
                            } else {
                            hasEmail = true;
                            break;
                            }
                            }

                            if(hasEmail) {
                            Server.Transfer("addlistingpost.aspx", true);
                            }
                            else
                            {
                            Response.Write("<script language='javascript'>window.alert('you can not enter email id in company profile.');window.location='addlisting.aspx';</script>");
                            }


                            I basically left the logic of checking the whole string out of the loop.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 at 9:52









                            Sk83r1l4m4

                            478




                            478






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409070%2fhow-to-validate-textbox-not-to-accept-emailids%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Berounka

                                Sphinx de Gizeh

                                Different font size/position of beamer's navigation symbols template's content depending on regular/plain...