chetan kambli
chetan kambli

Reputation: 814

How to validate textbox not to accept emailids

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 [email protected], 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 [email protected] 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?

Upvotes: 1

Views: 76

Answers (4)

Enigmativity
Enigmativity

Reputation: 117084

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 [email protected] 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-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\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-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Upvotes: 2

Sk83r1l4m4
Sk83r1l4m4

Reputation: 153

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.

Upvotes: 1

Sagar Shirke
Sagar Shirke

Reputation: 686

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

Upvotes: 1

Serg
Serg

Reputation: 22811

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

Upvotes: 1

Related Questions