Email Address Validation. Is this Valid? - asp.net

I am using the asp.net validation control to validate email addresses. There was an issue with an email address because it has the following characters. "-." For example the email address w.-a.wsdf.com will not validate due to the ".-" in it. I was looking around for email standards that forbid this but could not find any. Should I change the asp.net regex to a custom one to allow this or is this not a valid email address?
The regex i am using now is : \w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*

You'll get loads of answers for this. This one has always served me well:
/\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
Originally found it in Michael Hartl's Rails book

^[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]$

Regular expressions are great but, as my comments showed in other answers, validating email addresses with them is not an easy task at all.
Based on my experience, I would suggest to either:
stick to the simplest email validation regex, ^.+#.+$ and send a confirmation email with an activation link;
or
avoid using regular expressions at all and use a library like my EmailVerify for .NET, which exposes a custom, fine tuned finite state machine for validating email addresses according to (all of) the related IETF standards plus disposable, DNS, SMTP, mailbox tests.
Obviously you can mix the two alternatives and perhaps avoid using regular expressions but send a confirmation link anyway.
Disclaimer: I am the tech lead of EmailVerify for .NET, a Microsoft .NET component for validating email addresses.

The regex below forbids w.-a.wsdf.com
using System.Text.RegularExpressions;
public static bool IsValidEmail(string email)
{
return Regex.IsMatch(email, #"\A[a-z0-9]+([-._][a-z0-9]+)*#([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,4}\z")
&& Regex.IsMatch(email, #"^(?=.{1,64}#.{4,64}$)(?=.{6,100}$).*");
}
See validate email address using regular expression in C#.

Take a text input in html and a button input like this
Now when the button is clicked then the JavaScript function SubmitFunction() will be called. Now write the bellow code in this function.
function checkEmail() {
var email = document.getElementById('txtEmail');
var filter = /^([a-zA-Z0-9_\.\-])+\#(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}
*its works *

Related

Firestore Security Rules - check if field is a valid email address

How can I verify if an incoming field is a valid e-mail? Is there a way to use string-functions or anything in Firestore security rules?
Example:
Let's say I have a Create-Request with a field called "email". In my Firestore security rules, I would like to check if the email is a valid email address:
contains '#'
ends with either .xx or .xxx (a casual country-domain-ending)
has a '.' before the last three or two letters of the email
the '.' does not follow directly after the '#' - at least two letters have to be in-between
So that e.g. example#emailprovider.com gets accepted, but not example#.com.
I know that this check is quite extensive and further would like to know if it makes sense to introduce such a validation to security rules?
You can use rules.String.matches.
See
https://firebase.google.com/docs/reference/rules/rules.String#matches
https://github.com/google/re2/wiki/Syntax
How to validate an email address using a regular expression?
Performs a regular expression match on the whole string.
A regular expression using Google RE2 syntax.
If you want to set only email address then It's necessary to validate the field as email address.
I found an example of a regex (and adjusted a bit):
^[a-zA-Z0-9._%+-]+#[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,5}$
The source of this is at the bottom of the following page:
https://firebase.google.com/docs/reference/security/database/regex
You should also take into account the note as well:
Note: THIS WILL REJECT SOME VALID EMAILS. Validating email address
in regular expressions is difficult in general. See this site for
more depth on the subject.

Adding IP Address to Email Validation RegEx

I am using the RegEx "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*#[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+[.])*(\.[a-zA-Z]{2,17})$"to validate Email but my lead want to validate as per the Microsoft standard. SO i need to follow
http://msdn.microsoft.com/en-us/library/01escwtf(v=vs.100).aspx
In that everything working fine as per the standard but still i am facing the issues with
Valid: js#internal#proseware.com
Valid: j_9#[129.126.118.1]
the above mentioned mail ID is still returning as invalid. I tried using the regex used in that page
^(?("")(""[^""]+?""#)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])#))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$
but i am getting the error in the server page. Though I pasted the expression inside the validation Expression it can't able to accept the characters.
Note : am using ASP.Net validators for validating the email.
Description
To match both of those email addresses in your sample text, I think I would rewrite your expression like this:
[A-Z0-9._%#+-]+#(?:[A-Z0-9.-]+\.[A-Z]{2,4}|\[(?:(?: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]?)\])
If you're looking to use this to validate a string which may contain only an email then you can add the start/end of string anchors ^ and $.
Example
Live Demo
Sample Text
Valid: js#internal#proseware.com Valid: j_9#[129.126.118.1]
Matches
[0][0] = js#internal#proseware.com
[1][0] = j_9#[129.126.118.1]

razor string manipulation

I have a razor string
#postername.Substring(0, #postername.IndexOf("#"))
If the username has email I get the username before # sign but if the username doesn't have email I want to have that whole word, how to do ?
if(#postername.Contains("#")){
#postername.Substring(0, #postername.IndexOf("#"))
}else{
#postername
}
but didn't work, pls help
If you must do this at View level, build the logic into a variable first:
#{
string posternameShort = postername;
if(postername.Contains("#")){
posternameShort = postername.Substring(0, postername.IndexOf("#"))
}
}
Then call the new variable:
#posternameShort
To further clarify this, Razor functions (what appears in the code block between { and }) fully support C# code. Additionally, Visual Basic code is supported as well.
This is powerful. Any string manipulation that would occur in C# or Visual Basic, is also available to use in Razor code.
More info on this available here and here.
Curt's response outlines a basic way to do this, but I thought it would be good to explain the "why" behind the "how," especially since I've been looking into how to manipulate strings with Razor myself.

Internet e-mail validation expression validates everything

I'm validating input in a asp.net page but the problem is it validates e-mails like hasangürsoy#şşıı.com
My code is:
if (Regex.IsMatch(email, #"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"))
{ valid }
else { invalid }
EDIT:
I've written a question before especially to validate e-mail addresses including Turkish characters but now I don't want users to be able to input mails with Turkish characters because users mostly type Turkish characters by mistake and I cannot send mails to these addresses.
Why don't you just use build-in System.Net.Mail.MailAddress class for email validation?
bool isValidEmail = false;
try
{
var email = new MailAddress("hasangürsoy#şşıı.com");
isValidEmail = true;
{
catch (FormatException x)
{
// gets "An invalid character was found in the mail header: '.'."
}
RFC3692 goes into great detail about how to properly validate e-mail addresses, which currently only correctly handle ASCII characters. However this is due to change, and hence your validation should be as relaxed as possible.
I would likely use an expression such as:
.+#.+
which would ensure you're not turning people away because your regular expression gives them no choice.
If the e-mail is important you should be following it up with verification usually done via sending an e-mail to the supplied address containing a link.
I recommend you reading this:
http://www.codeproject.com/KB/validation/Valid_Email_Addresses.aspx

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