Regular expression for e-mail validation not working - asp.net

I've set up a regular expression validator to validate all e-mail addresses that are entered into a textbox on one of my forms the only problem is it's not working in a weird case.
Here's my code
<asp:TextBox ID="tbEmail1" runat="server" />
<asp:RegularExpressionValidator Display="Dynamic" ID="rgv1" runat="server"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="tbEmail1" ErrorMessage="Invalid email address" />
The case that this is not working is when an e-mail address is entered like this
jamie-taylor-#hotmail.co.uk
Now I wasn't aware that e-mail addresses could be set up in this way but i've just set one up and it seems to be fine except the fact that I cannot input this into my form without it telling me it's invalid
Any ideas?
Thanks in advance

Comparing E-mail Address Validating Regular Expressions. This page has both regexes and e-mail addresses to test with.
It is generally better to make your regex not very strict. Better to have one or two e-mails bounce back than that your customers can not create an account.

let's enhance the regex :
\w+([-+.']\w+[-]*)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
edit
for #Sjoerd :
\w+([-+.']\w+[-]*)*#\w+([-.]*\w+)*\.\w+([-.]\w+)*

use the below expression may help you
\w+([-+.']\w+[-])*#\w+([-.]\w+)*\.\w+([.]\w+)*

Related

RegEx stops working when placed into ValidationExpression

I have a text box where a user enters their email address. I need to prevent people using certain email addresses, as this is for corporate (B2B) use.
Can anyone help me with the RegEx which would return false if email addresses contain #gmail or #yahoo?
So far I have this (thanks to #Sabuj) #(yahoo|gmail)\. but when placed into a RegularExpressionValidator it doesn't work:
<asp:RegularExpressionValidator ValidationExpression='#(yahoo|gmail)\.' runat="server" ControlToValidate="txt_email" />
Having read MSDN for more info, I've also tried this but it still returns true regardless of the content entered:
<asp:RegularExpressionValidator ValidationExpression='^(#(yahoo|gmail)\.)$' runat="server" ControlToValidate="txt_email" />
Since e-mail addresses have a complex syntax (more complex than most people realise, for instance, they can contain comments [RFC 822 ยง 3.4.3]), I'd suggest not using regex at all for this. Instead, use a "proper" e-mail parser, then ask the parser for the domain part of the address.
Use this:
<asp:RegularExpressionValidator ValidationExpression=".*#(?!(yahoo|gmail)).*" ControlToValidate="txt_email" runat="server" ErrorMessage="Yahoo and Gmail disallowed"></asp:RegularExpressionValidator>
The validation expression property should be set to match the entire string.
But my regex .*#(?!(yahoo|gmail)).* matches the whole email. So it works :)
You don't need ^ or $ since the string is gonna be a single line.
Also don't forget to add type="email" to your txt_email. It will automatically take care of whether it is a valid email or not.
If the error msg appears, then it isn't valid, but if it doesn't appear, then it is absolutely valid.
I've come up with ^.*#(?!(yahoo|gmail)).*$
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationExpression="^.*#(?!(yahoo|gmail)).*$" runat="server" ControlToValidate="txt_email" Text="No free email accounts allowed" />
This will allow any text to pass the validator that doesn't contain #yahoo or #gmail.
Don't forget to check Page.IsValid in your code behind, and to include an <asp:ValidationSummary runat="server" /> in your .aspx.
You can use this regex to check whether the mentioned emails are containing or not:
#(gmail|yahoo|mailinator|guerrillamail|dispostable)\.

How to check in an asp text box has # sign

I'm developing a module where I need to take input from a user one of each is users email. In order to filter out a wrong input I want to check if a corresponding checkbox has an # sign.
Does anyone knows how can I check it using vb.net ?
Aside the main question to check if a string contains the # character and considering that you are getting your string from a user input, in your case I think is better to check if the user entered a valid mail address.
To do so there are some solutions all reported in this other SO question How do I validate email address formatting with the .NET Framework?
In ASP.NET you can use RegularExpressionValidator for this.
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server"
ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ControlToValidate="tbEmail"
ErrorMessage="Invalid Email Format">
</asp:RegularExpressionValidator>

asp.net validation when field is not required

I need to set validation on a textbox where the user types in their email address... This is not a required field though so I want to allow the form to be submitted if the textbox contains the default text ("Email address").
I've posted the code i have already to ensure a valid email address is typed.
<asp:RegularExpressionValidator CssClass="errorpopup" Display="Dynamic" ID="regexpEmail"
ValidationGroup="mySubmit" runat="server" ErrorMessage="<strong>Please enter a valid email address.</strong>"
ControlToValidate="tbEmail" ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
SetFocusOnError="true" />
Just enclose the entire regex in (?:...)? to make it optional.
But you're not using a very good e-mail validation regex. Aside from the fact that e-mail addresses can never reliably be validated by regular expressions, you could do a little better by using
^(?:[\w.%+-]+#(?:[\w-]+\.)+[A-Za-z]{2,6}\s*|Email address)?$
This will still not catch all valid addresses, and will match some invalid addresses. But short of the RFC 2822 implementation regex which spans about four or five lines of code, this is probably a good compromise.

RegularExpressionValidator for multiple emails

I'm validating a textbox for valid email with this:
<asp:RegularExpressionValidator ID="RegularExpressionValidator1"
ControlToValidate="txtMailCustom"
Text="Invalid address"
ValidationExpression="^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]
{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$" runat="server" />
Now I want users to be able to put multiple email addresses , separated by a comma and space.
How can I integrate that behaviour ?
Try the expression below, which works for me:
((\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*)*([,])*)*
The above code is for ,, separating e-mail addresses.
If you would like to use ; instead of ,, than replace , with ; at the end of the above expression.
try this expression:
^(\s*,?\s*[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*#([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})+\s*$
I should do this: what it does is check for at least one mail, and for list of email before it with each a comma and possibly a space.
^(([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?), ?)*^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
Use this if the space between each mail is required:
^(([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?), )*^([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$
Note I just used your single mail checker as a base and didn't really edit it.

ASP.NET email validator regex

Does anyone know what the regex used by the email validator in ASP.NET is?
Here is the regex for the Internet Email Address using the RegularExpressionValidator in .NET
\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*
By the way if you put a RegularExpressionValidator on the page and go to the design view there is a ValidationExpression field that you can use to choose from a list of expressions provided by .NET. Once you choose the expression you want there is a Validation expression: textbox that holds the regex used for the validator
I don't validate email address format anymore (Ok I check to make sure there is an at sign and a period after that). The reason for this is what says the correctly formatted address is even their email? You should be sending them an email and asking them to click a link or verify a code. This is the only real way to validate an email address is valid and that a person is actually able to recieve email.
E-mail addresses are very difficult to verify correctly with a mere regex. Here is a pretty scary regex that supposedly implements RFC822, chapter 6, the specification of valid e-mail addresses.
Not really an answer, but maybe related to what you're trying to accomplish.
We can use RegularExpressionValidator to validate email address format. You need to specify the regular expression in ValidationExpression property of RegularExpressionValidator. So it will look like
<asp:RegularExpressionValidator ID="validateEmail"
runat="server" ErrorMessage="Invalid email."
ControlToValidate="txtEmail"
ValidationExpression="^([\w\.\-]+)#([\w\-]+)((\.(\w){2,3})+)$" />
Also in event handler of button or link you need to check !Page.IsValid.
Check sample code here : sample code
Also if you don't want to use RegularExpressionValidator you can write simple validate method and in that method usinf RegEx class of System.Text.RegularExpressions namespace.
Check example:
example
For regex, I first look at this web site: RegExLib.com
Apart from the client side validation with a Validator, I also recommend doing server side validation as well.
bool isValidEmail(string input)
{
try
{
var email = new System.Net.Mail.MailAddress(input);
return true;
}
catch
{
return false;
}
}

Resources