[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.
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
guys, I don't know this question is already present or not but I have tried every search, so my question is why my regex is not working properly in RegularExpression attribute. this same regex I have used in javascript and this is working on javascript. can anyone help me what I am doing wrong here?
[Required]
[Display(Name = "First name")]
[MaxLength(50)]
[RegularExpression("^(?![#\\+\\-=\\*])", ErrorMessage = "First Name Should not start with these characters #, +, =, *, -")]
public string firstname { get; set; }
I am using this regex for validating the First Name should not start with #,+,=,*,-.
I have already spent 3 hours to figure out what I am doing wrong here.
I believe you regex should look like this:
^(?![#\\+\\-=\\*]).*
Here is a working example.
Your regex is invalid. Here is the updated one, which works as you 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.
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.")]
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();
}