How would I check on registration that a user types in a specific email address? For example, i want my registration form to only allow these email addresses:
#gmail.com
#yahoo.com
#live.com
Like this:
[RegularExpression( #"#(gmail|yahoo|live)\.com$", ErrorMessage = "Invalid domain in email address. The domain must be gmail.com, yahoo.com or live.com")]
public string EmailAddress { get ; set ; }
You don't even need a regular expression; you can just use the split() function to obtain the part of the email address after the "#" and check it against your list of allowed providers.
This by itself doesn't guarantee that it's a well-formed email address (that may require a regex, and a somewhat complicated one), but it will make sure that the address ends with one of the domains on your list.
You could use a RegularExpressionValidator control and an expression to look for the email domains. You can find a sample at http://www.regexplib.com if you don't already have one.
You're probably going to want a CustomValidator as well that performs an identical server-side check. Users can circumvent your RegularExpressionValidator if they disable Javascript.
There is not built in but you can use [RegularExpression].You can write custom EmailAttribute deriving from RegularExpressionAttribute.
A very well implementation is done here
You can use following regular expression to check email:
^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)#[a-z0-9]+(\.[a-z0-9]+)\.([a-z]{2,4})$
Beside this, you can have Data Annotation Extension which has [Email] attribute that allows for validating an email address.
Related
I have a user input to supply website address, obviously most users have no idea what is well formatted url so I look for a website address Regex that will follow this rules:
1) www.someaddress.com - True
2) someaddress.com - True
3) http://someaddress.com - True
4) https://someaddress.com - True
5) https://www.someaddress.co.il - True
6) http://www.someaddress.com - True
I use this Regex:
[RegularExpression(#"^((http|ftp|https|www)://)?([\w+?\.\w+])+([a-zA-Z0-9\~\!\#\#\$\%\^\&\*\(\)_\-\=\+\\\/\?\.\:\;\'\,]*)?$", ErrorMessage = "Not a valid website address")]
public string SiteUrl { get; set; }
But it's useless because it allows almost every string to pass.
Please supply a data annotation answer and not answers such as:
Uri.IsWellFormedUriString
Because .net doesn't support client side validation for custom attributes.
There is a UrlAttribute to validate URLs, but it does enforce the protocol being there, which it appears you don't want.
However, the source code is available and it does use a regular expression that you can steal and modify. Modifying just the protocol portion to be optional the way you want, you get this:
^((http|ftp|https)://)?(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7F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;=]|:|#)|\/|\?)*)?$
(Side note: I noticed that your regex allowed www://, which is suspicious. I took it out in this, but if you truly do need that, then you can add it.)
These are values I tested with:
www.someaddress.com Yes
someaddress.com Yes
http://someaddress.com Yes
https://someaddress.com Yes
https://www.someaddress.co.il Yes
cow No
hi hello.com No
this/that.com No
In the comments of the source code it does say:
This attribute provides server-side url validation equivalent to jquery validate, and therefore shares the same regular expression. See unit tests for examples.
This is an odd (to me anyway) query string problem.
I'm using a installation tool that has web serial number validation. Basically the install passes a users email and serial number to a web page (or a controller method for MVC) and that takes the query string arguments and does magic to validate the installation.
One of the arguments is the email address passed in the query string.
I recently has a user who used ‘+’ email addressing to purchase a subscription. All worked well until he went to install the product and had to get past the validation screen.
After doing some digging I found that instead of receiving
‘joe+foo#gmail.com’
The validation code receives
‘joe foo#gmail.com’
Of course the space ruins the validation attempt as his email address is now wrong.
I've spoken with the install tool company (Advanced Installer, best install tool on the planet) and they claim (and I believe them) that the email is sent correctly.
So that leaves me at how do I get the asp.net mvc querystring parser do to the right thing for that particular argument and pass the string with the '+' to the contoller method ?
It's asp.net mvc 5 if it matters.
Thanks for any help you can spare.
UPDATE
The problem is with the sending, not the reception. the plus sign ends up unencoded so it translate to a space= when it get handled by the query string parser.
So what I am looking for is a way to customize the query string parser for that particular URL (and only that URL).
The shortcut to a fix is to replace spaces with a plus sign in the email arg. Simple, but I hate that kind of hackery in my code I;d prefer to have it use a customized parser to that if I need it else where I can just plug it in any way it goes.
You can customize just about everything else in asp.net mvc so I was wondering if there was a way to do the query string pasring in some custom fashion.
Assuming you are calling the URL from javascript, instead of doing this:
url += "?email=" + email;
Encode the value like this:
url += "?email=" + encodeURIComponent(email);
If you are calling the URL from the server, then:
string encodedEmail = Server.UrlEncode(email);
UPDATE
If you can't change where the URL is getting called, then you don't have any other option than:
HttpUtility.UrlEncode(Request.QueryString["email"]);
or:
email = email.Replace(' ', '+');
It looks like I'm going to have to go with my hack solution of swapping space for a plus sign in that particular query string parameter. Not the ideal solution in my way of thinking, but it will do the trick.
SmtpClient.Send(...) is OK with simple email addresses (eg "JoeBlow#MyCo.com") or fancier addresses (eg "Joe Blow <JoeBlow#MyCo.com>").
But my company has its address book entries formatted like "Blow,Joe <JoeBlow#MyCo.com>". SmtpClient chokes on this because it assumes that the comma is dividing two separate addresses. Is there a way to tell Smtp that I am not using commas in this way? Or is the only option to reformat the company addresses? Don't tell me to just eliminate everything other than the actual address because this extra name info is very useful.
You need to use a string like "\"Blow, Joe\" <JoeBlow#MyCo.com>" so that the comma is within a quoted-string.
In .NET the MailAddress object has both an actual address field and a display name field. The display name CAN have commas (even if this is not encouraged). Using this object with the constructor with 2 parameters allows these funny display names to be used.
How can I validate the following incorrect email addresses with regex in c#?
eg:
test#test.com.net.com
or
test#test.net.net.org
These are being validated as correct email addresses. Any thoughts? Thanks
While both test#test.com.net.com and test#test.net.net.org are valid, from a syntactic point of view, their domain parts do not point to existing domains.
For this kind of test, you may want to extract the domain part you are interest in and query the DNS (see RFC 2821 and RFC 2822) to see if it exists.
Since you are using .NET, by the way, I would suggest you to take a look at our EmailVerify.NET, a leading email validation library which can validate the syntax (according to the latest IETF standards), the domain parts and the presence of a mailbox for your email addresses.
You may want to just use something like:
[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?
For a list, please see this page.
Please consider this regex:
([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})
It will match all the email-address, if it did not match the email address, the email-ad is not in correct format, Hope it helps :)
I'm trying to make a form on asp.net where you can send emails. I want to have a textbox where I can type all email addresses which I want to send a message...
However I want to add a validator there so the user always has the correct syntax and therefor, use that string on c# and send the message properly.
For now, I have a validator for a single email address...
<asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server"
ControlToValidate="tbxAlClEmail" ErrorMessage="E-mail Invalido"
Font-Bold="True" ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*"
ValidationGroup="vgpNuevoCliente">
</asp:RegularExpressionValidator>
so, we can see that....
email = \w+([-+.']\w+)#\w+([-.]\w+).\w+([-.]\w+)*
(I know that it is difficult to find an expression regular that can validate all kind of email addresses... but this will have to do)
now I want to addapt this regular expression to an asp.net validator:
email ( (,|;) [SPACE]* email )*
example of results i want:
john#hotmail.com, amy#yahoo.com,diana#hotmail.com; alicia#gmail.com
I hope you can help me with that...
Thanks in advance
What you have won't work to validate email addresses.
Using regex for email validation is extremely hard to do right and there are literally hundreds of examples on the net of validators that get close... but still aren't perfect.
But that's only a small part of the problem anyway. Your absolute best bet is to drop trying to validate the address and simply send the messages while telling the user which addresses failed.
If you are doing it right, then you aren't including every address in the TO field anyway and instead are sending distinct messages to each individual address. Which would make it fairly easy to report errors.
Of course, even then numerous mail servers are configured to not even respond if a bad email address is sent to it and instead they just black hole the message. No amount of validation etc is going to get past that.
For fun, you might want to read the following in its entirety so that you understand the full problem: http://www.regular-expressions.info/email.html