Reputation: 2368
I have the following JSON:
validate = {
'(\\.org|\\.info|\\.biz|\\.name)$': [
{ 'type': 'size', 'pattern': /^.{3,64}$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
],
'.*': [
{ 'type': 'general', 'pattern': /^[^\.-].*[^\.-]$/, 'message': 'Your domain name shouldn\'t contain . or - at the beginning or the end.' },
{ 'type': 'characters', 'pattern': /^[abcdefghijklmnopqrstwuvxyz0123456789]+$/, 'error': 'Your domain can have at max 26 characters and at least 3.' }
]
};
and tried to use like this:
var validate = new Dictionary<string, dynamic> {
{
@"(\.org|\.info|\.biz|\.name)$",
new {
Type = "size",
Pattern = @"^.{3,64}$",
Message = "Your domain can have at max 26 characters and at least 3."
}
}
};
Where the key of the dynamic object its the regex pattern for the domain extension and the regex inside Pattern
key its the one that should match the domain name.
But I can't figure out how I put 2 validations type inside the dynamic
part of the Dictionary
.
Has anyone did anything like it before or it's stupid and I should do in another way?
The point of doing it this way it's that I can serialize the dictionary as a Json.
Upvotes: 1
Views: 326
Reputation: 2368
I tried with a List<dynamic>
at the dynamic part of my Dictionary:
var validate = new Dictionary<string, List<dynamic>> {
{
"(\\.org|\\.info|\\.biz|\\.name)$",
new List<dynamic>
{
new {
Type = "size",
Pattern = @"^.{3,64}$",
Message = "Your domain can have at max 26 characters and at least 3."
}
}
},
{
".*",
new List<dynamic>
{
new {
Type = "general",
Pattern = @"^[^\.-].*[^\.-]$",
Message = "Your domain name shouldn\'t contain . or - at the beginning or the end."
},
new {
Type = "characters",
Pattern = @"^[abcdefghijklmnopqrstwuvxyz0123456789]+$",
Message = "Your domain name should contain only alphanumeric characters."
}
}
}
};
And using Json
from JsonResult
mvc3 view it returned the json I needed.
Upvotes: 1
Reputation: 15931
if you are trying to do this for an MVC site I think you should look into custom validation, and let the runtime handle the plumbing for you. This question which discusses how to implement a custom RegularExpressionAttribute, which seems here.
Upvotes: 1