How to validate textbox not to accept emailids

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 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?


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

Email Validation, How check email ID is valid or not in JavaScript? Other validation : Age > 100 or 125 check if the length is not more than 3 digits. Mobile number length must not be more than 10 digits and less than 10 digits. Validation is required before accepting the data because, in future, we process and generate the reports as per the received data.


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-\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])+)\])

Form data validation, I have a text box where a user should enter his email id. How do I validate it that it accepts only email id and if email id does not contain '@' I  In this tutorial you will learn how to allow a user to enter only alphabets and charater or string type value in a text-box where numeric values are not allowed to enter.


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

how to validate a textbox to accept only email id in vb2010, Using this type of field instead of the regular text field the browser uses a regular Does this means that the user cannot type in a fake email address? No. VB.Net - Input Validation for TextBox Controls in a Windows Forms. [-Online Programming Course-] Please check out my online course on Udemy: Visual Basic .Net, Access Database, EAN-13 Barcode


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.

Validating email addresses, Yes you can use Javascript and Regex to accomplish your task, here is an example. Hide Copy Code. function validateEmail(elementValue)  Email validation Validating email is a very important point while validating an HTML form. In this page we have discussed how to validate an email using JavaScript :


text box accepting email address only, Note that this is not even close to an adequate filter for valid e-mail addresses; it would allow things such as " @beststartupever.com" (  First I wnat to validate textbox to accept 500.00 like this. Second I want that user should be able to enter only two digit after decimal point. Also I want to restrict character value. Pls make a function and validate these condition. So that I could call this function on each textbox. asp.C# controls not html thanks


<input type="email">, mysite@.com.my [ tld (Top Level domain) can not start with dot ". To get a valid email id we use a regular expression /^\w+([\.-]?\w+)*@\w+([\.


JavaScript : email validation, The easiest and safest way to check whether an email address is well-formed is to use PHP's filter_var() function. In the code below, if the e-mail address is not