ASP.NET MVC 5 EmailAddress attribute not working properly - c#

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

Related

C# Regular Expression Attribute not working as expected

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.

Prevent html tags entries in mvc textbox using regular expression

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.")]

Without disabling validation, how can I encode this .Input field containing the "<" and ">" signs?

I have an input field in a form that accepts text from a user. The user in question will always copy/paste in text from a auto-generated e-mail that includes "<" and ">" around text. This will throw an error since the text will be recognized as HTML. Without disabling validation (by ValidateInput(false)), how can I encode this .Input field my users can copy/paste the text with the "<" and ">" signs?
<%=Html.Input(sample => sample.SampleInput)%>
Use the [AllowHtml] attribute on your model property to allow HTML code to pass validation.
Please note that this is a possible security issue since the submitted input could contain scripting code. This is why it's prohibited by default. So take care of the input you receive to ensure it's safe.
http://msdn.microsoft.com/en-us/library/system.web.mvc.allowhtmlattribute(v=vs.98).aspx
You can use New Request validation features in ASP.Net 4.5
<httpruntime requestvalidationmode="4.5" />
Or you can [AllowHtml] on your Model Object
public class User{
[AllowHtml]
public string email {get; set;}
}
Or you can just switch off validation for a single action if you like.
[ValidateInput(false)]
public ActionMethod Edit(User user)
{
// Do your own checking of value since `user` could contain XSS stuff!
return View();
}

Checking for specific email addresses using data annotations

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.

Unable to replace placeholder {0} in ErrorMessage of a RegularExpressionAttribute

[StringLength(100),
RegularExpression(RegexPatterns.NoBracketsRegEx,
ErrorMessageResourceType = typeof(Resources),
ErrorMessage = "HTML tags are not allowed in {0} field")]
public virtual string Title { get; set; }
When I try to enter Html tags in Title field I am getting the error message:
"HTML tags are not allowed in {0} field"
instead of
"HTML tags are not allowed in Title field".
I am using System.ComponentModel.DataAnnotations, Version=4.0.0.0
I have tried setting Display(Name="Title") but still no luck!! Any idea what's going wrong?
You can't use ErrorMessage and ErrorMessageResourceType together. Their use is mutually exclusive.
For non-localized error messages, you can use the ErrorMessage property initialized with a string literal (without format specifiers because, like you've discovered, they will be shown as is).
For localized error messages, use the ErrorMessageResourceType property together with the ErrorMessageResourceName property.
Here's are a couple of related blog posts that may help:
Localizing Validation Using DataAnnotations, and
ASP.NET MVC 2: Model Validation.

Categories

Resources