I am using the RegularExpression attribute to verify multiple email addresses on one input in my view model. The ErrorMessage keeps coming up on field. I have validated my RegEx on 5 different online test sites and they all test positive.
Here is my code:
[RegularExpression(#"\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*([,;\s]+\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*){0,7}", ErrorMessage = "Please enter a valid email address. For multiple addresses please use a comma or semicolon to separate the email addresses.")]
public string EmailAddresses { get; set; }
If I enter an email address it works, if I enter two email addresses without spaces it works, but if I add a space it breaks. I added the '\s' to include white spaces and it does work on the online testers I have tried but it will not work in my application.
The expected valid result should be:
'test#test.com, test2#test.com, test3#test.com'
However, this it coming back as invalid. If I use the exact same sequence with no spaces it is valid.
Kendo UI is checking the validation of the form before sending it to the controller.
Any help is very appreciated. Thank you in advance.
Try this pattern:
"^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"
You don't need "#" symbol. I tested your pattern at http://regexstorm.net/tester and it is correct match for test#test.com, test2#test.com, test3#test.com . Use construct below
public class LoginViewModel
{
[RegularExpression("\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*([,;\s]+\w+([-+.']\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*){0,7}", ErrorMessage = "Please enter a valid email address. For multiple addresses please use a comma or semicolon to separate the email addresses.")]
public string EmailAddresses { get; set; }
}
For it to work in ASP.NET MVC Framework ensure you do the have the following on the view
<input type="text" id ="EmailAddresses" name="EmailAddresses"/>
The id and name attributes are required for it to validate. You can do this manually as above or use #Html.EditorFor(model => model.EmailAddresses) which will create the id and name attributes for you
I found the issue. In the end it had nothing to do with C#. The problem was I was using Kendo UI on the front end, as updated in the main post. Keno UI had to also receive the RegEx update sent by Andrew to solve the issue. I will make Andrew as the correct solution as his RegEx did work for me. I was totally turn around by the server side/client side difference as I am new to this. Thank you all for your help.
Related
I use EmailAddress attribute. When you type tom#jerry it accepts as a valid email. When I type tom#jerry. only then it complains. How can I solve this problem so the validation complains when given input like tom#jerry.
You can implement regular expression on top of it
like this one is for white spaces
[RegularExpression(#"^\S*$", ErrorMessage = "Email Address cannot have white spaces")]
Here is the link for similar problem
ASP.NET MVC 5: EmailAddress attribute custom error message
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.
The scenario I have seems pretty common but I did not found good solution so far. So there's ASP.NET-MVC application with MSSQL database sitting in back-end. The model includes class A with string field Description that needs to be limited to 100 characters. It's declared as follows:
[StringLength(100)]
public virtual string Description { get; set; }
The corresponding column in the database is nvarchar with column length = 100. In the UI, Description field is represented by textarea with maxlength = 100.
Now the issue comes when there're line breaks in the textarea. They are interpreted as 1 character on client side but on server side they are represented by Environment.NewLine which is 2 characters (\r\n). So it exceeds the database column length (actually server side validation fails before the whole back-end thing but let's omit the validation for simplicity).
The solutions I found so far are:
Add some magic on client side in order to interpret line break in
textarea as two characters. I don't like this solution as it can
confuse a user.
Replace \r\n with \n on server side. Seems like a hack that could
have some side effects.
Remove/increase column length in the database. The simpliest one
(without taking server-side validation issue into account) but let's
say it's not the case.
I guess there should be something else besides those three.
This is an old and known issue with MVC (I am not sure if it was solved)
but different browsers treat line break differently. My suggestion is to custom model binder like what you find here jQuery validate textarea maxlength bug
I want to prevent any html tags (written between "<>") in a textbox in my mvc 4 application.
I have given the data annotation regular expression for my property as follows:
[RegularExpression(#"<[^>]*>",ErrorMessage="Invalid entry")]
public string Name { get; set; }
But the regular expression not working correctly. When I type , it shows "Invalid entry".After that, when I type some normal text like "praveen" also shows "Invalid entry" error message.
I have tried another regular expressions something like #"<[^>]*>" ,but same result as above.
Please help.
You have to logic turned around. The regex you have written is what you do not want to allow, whereas the RegularExpression attribute requires you to enter what you do allow. Anything not matching your regex will show the ErrorMessage.
An alternative regex could be:
#"[^<>]*"
which would disallow < and >.
RegularExpression to avoid any html tags entry use:
[RegularExpression("^[^<>,<|>]+$", ErrorMessage = "Html tags are not allowed.")]
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.