I'm trying to use the "RegularExpression" DataAnnotation validaiton in my model. My problem is even for the valid input I provide for the field containing the validation, the regex doesn't match and it fails.
I try to test the same valid input with the regex in a stand-alone console app and I find it gets through.
My point is, when regex is being used in dataannotation as validation, it considers all input as bad input. What am I missing here?
My regex is for checking comma-seperated email IDs. Here is my model:
public partial class BuildModel
{
public Int64 ConfigID { get; set; }
[Required(ErrorMessage = "Please select a stream!")]
public String Name{ get; set; }
[RegularExpression(#"^(([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})(\]?)(\s*(;|,)\s*|\s*$))", ErrorMessage = "Please enter valid email IDs separated by commas")]
public string EmailIDs { get; set; }
public bool IsActive { get; set; }
}
Note: The "Required" data-annotation validation works just fine. It is just the Regex one, that won't work!
As far as i understand you just have problem within your Regex.
To validate emails separated by comma you can use this regex:
^( *[\w+-.%]+#[\w-.]+\.[A-Za-z]{2,4}( *, *)? *)+$
Check in in here.
You must the jquery.validate.min.js and jquery.validate.unobtrusive.min.js in your page without these libraries required annotation would not works.
Related
I am trying to write a regular expression that replaces a data annotation adding more paramter to it.
For example,
Given this property
[DataMember(Name = "users")]
public List<User> Users { get; set; }
I would like to change it to
[DataMember(Name = "users"), XmlNode("users")]
public List<User> Users { get; set; }
Any idea how to achieve this?
search with this regex:
\[DataMember\(Name = \"(.*)\"\)\]
and replace by this one:
\[DataMember\(Name = "$1"\), XmlNode\("$1"\)\]
regex 101 link
I am using the regular expression for the Password field. Includes alpha-numeric string and md5.
[Required(AllowEmptyStrings = true)]
[RegularExpression(#"^(((?=.*\d)(?=.*[a-zA-Z]).{6,20})|([0-9a-f]{32}))$")]
[Display(Name = "Password")]
public string Password { get; set; }
And, I'd leave the field empty password. But ValidationMessage shows "This field is required."
message.Even if delete Required, it still gives the message.
Friends, how can I fix it?
Removing the Required attribute worked for me for server-side validation.
I've never used asp.net, and know nothing about it. However, 10 seconds on google tells me:
Make it nullable ( public string? Passsword { get; set; } )
I am using asp.net mvc 3 and I have this regex validation check in my model:
[RegularExpression(#"/^[1-7]$/", ErrorMessage = "Please enter a valid day number")]
public string DayNr {get;set;}
the validation check does not work however:( what is incorrect in the above code?
it should be something
[RegularExpression(#"^[1-7]$", ErrorMessage = "Please enter a valid day number")]
public string DayNr {get;set;}
for number validation go over this link.
Mvc validation regular expression only numbers?
Do this - much simpler, more readable, easier to maintain:
[Display(Name="Day Number")]
[Range(1, 7, ErrorMessage = "{0} value must be between {1} and {2}")]
public string DayNr { get; set; }
Hope this helps
I want to use a regex validator to ensure that a certain string variable contains the substring "www.youtube.com/watch?v=", how would I do this?
[RegularExpression()]
[Required(ErrorMessage = "Youtube link is Required")]
[StringLength(100, ErrorMessage="Youtube link cannot exceed 50 characters")]
public string YoutubeLink { get; set; }
if (YoutubeLink.Contains("www.youtube.com/watch?v="))
{
//...
}
http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx
To use the RegularExpression attribute, you must specify the regex to use :
[RegularExpression("www\\.youtube\\.com/watch\\?v=", ErrorMessage = "Link is incorrect")]
public string YoutubeLink { get; set; }
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.regularexpressionattribute.aspx
I think what you might want is to check for any type of URL beginning. In that case, you would use #"^(http://)?(www\.)?youtube\.com/watch\?v=" as the regex string.
See an example of what this would match
Maybe much easier to use something like (Python):
ytlink = 'www.youtube.com/watch?v='
if(ytlink in yourfrase):
doYourLogic()
else:
smthElse()
Please correct me if I understood your question wrong.
I have a simple view model class for MVC2 that has a MagicItem property:
public class VoodooViewModel {
[Required(AllowEmptyStrings = false,
ErrorMessage = "The Magic Item is required")]
[RegularExpression(#"^[^-]*$",
ErrorMessage = "Hyphens are not allowed in Magic Items.")]
public string MajorModel { get; set; }
}
I am simply trying to disallow hyphens in this property, but for the life of me I can't get it to work. Can anyone see what I'm doing wrong (the RequiredAttribute is working fine)?
To my eyes, the regex I have says "from the beginning of the string to the end, match anything that isn't a hyphen". I have tested this in the Regex tester here, and it works - but not in my code. I can't get the error to show no matter how many hyphens I put in it.
Like a tool, I forgot to check in the controller's action method to see if the ModelState was valid or not:
public ActionResult UberController(VoodooViewModel vvm)
{
if (!ModelState.IsValid) return View(vvm); //turns out this line is important
(...yaddayaddayadda...)
}
Thanks to Darin for pointing me in the right direction.