我正在使用asp.net mvc 5.我有List<string>
这样:
var animals = new List<string>
{
"Dog",
"Cat"
};
animals
只能包含2个值:Dog
和Cat
。因此,如果值为Tiger
或Lion
,该广告无效。
以下是我用来验证的基本方法:
var regex = new Regex(@"Dog|Cat");
foreach (string animal in animals)
{
if (!regex.IsMatch(animal))
{
// throw error message here...
}
}
现在,我想声明一个模型Animal
来存储List:
class Animal
{
//[RegularExpression(@"Dog|Cat", ErrorMessage = "Invalid animal")]
public List<string> Animals { get; set; }
}
并采取一些行动:
public ActionResult Add(Animal model)
{
if (ModelState.IsValid)
{
// do stuff...
}
// throw error message...
}
所以,我的问题是:在这种情况下如何使用正则表达式来验证List<string>
的值?
答案 0 :(得分:3)
您可以编写自己的属性:
export class ConditionBuilderComponent implements OnInit {
conditions: Condition[] = [];
catalog: Condition[] = [];
constructor(public _conditionService: ConditionService) { }
getConditions() {
this._conditionService.getConditions().then(conditions => this.catalog = conditions);
}
ngOnInit() {
this.getConditions();
}
stringify(o:any): string {
return JSON.stringify(o);
}
onChange(conditionsIndex, selectedCondition:string): void {
console.log(typeof selectedCondition);
//JSON.parse(selectedCondition);
console.log(selectedCondition);
//this.conditions[conditionsIndex] = this.catalog[condition];
console.log(typeof selectedCondition);
}
在public class ListIsValid : ValidationAttribute
{
public override bool IsValid(List animals)
{
var regex = new Regex(@"Dog|Cat");
foreach (string animal in animals)
{
if (!regex.IsMatch(animal))
{
return false;
}
}
return true;
}
}
课程中,您可以像这样使用它:
Animal
答案 1 :(得分:1)
定义自定义验证属性并在那里实现自定义逻辑。
<Grid x:Name="LayoutRoot" Background="{StaticResource AppBackGroundColor}">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="LoginImage.Height" Value="60" />
<Setter Target="LoginImage.Width" Value="60" />
<Setter Target="Headertxt1.FontSize" Value="25" />
<Setter Target="Headertxt2.FontSize" Value="20" />
<Setter Target="EmailTxtBox.Height" Value="30" />
<Setter Target="PwdBox.Height" Value="30" />
<Setter Target="LoginButton.FontSize" Value="25" />
<Setter Target="ForgotPwdButton.FontSize" Value="20" />
</VisualState.Setters>
</VisualState>
<VisualState>
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="720" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="LoginImage.Height" Value="90" />
<Setter Target="LoginImage.Width" Value="80" />
<Setter Target="Headertxt1.FontSize" Value="30" />
<Setter Target="Headertxt2.FontSize" Value="23" />
<Setter Target="EmailTxtBox.Height" Value="50" />
<Setter Target="PwdBox.Height" Value="50" />
<Setter Target="LoginButton.FontSize" Value="38" />
<Setter Target="ForgotPwdButton.FontSize" Value="24" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" MinHeight="517" />
</Grid.RowDefinitions>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".15*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Image Source="/Images/login.png"
Grid.Column="0"
x:Name="LoginImage" />
<Grid Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="Login"
Name="Headertxt1"
Foreground="{StaticResource AppFontColor}"
Grid.Row="0"
FontWeight="Bold" />
<TextBlock
Name="Headertxt2"
Foreground="{StaticResource AppFontColor}"
Grid.Row="1"
TextWrapping="Wrap" />
</Grid>
</Grid>
<Grid Grid.Row="3" Margin="0,25,0,0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox
Style="{StaticResource MediumSizeTextBoxStyle}"
Grid.Row="0"
Name="EmailTxtBox"
Height="50"
InputScope="EmailSmtpAddress"
Margin="12,0,12,10" />
<PasswordBox Name="PwdBox"
Grid.Row="1"
Style="{StaticResource MediumSizePwdBoxStyle}"
Margin="12,10,12,4"
Height="50" />
<Button Content="Login"
Grid.Row="2"
Margin="12,20,12,0"
x:Name="LoginButton"
BorderThickness="0"
Foreground="Black"
Background="White"
HorizontalAlignment="Stretch" />
<Button Content="Forgot Password"
Grid.Row="3"
Foreground="{StaticResource AppFontColor}"
FontSize="24"
Margin="0,10,0,10"
HorizontalAlignment="Center"
Name="ForgotPwdButton"
BorderThickness="0" />
</Grid>
<Image x:Name="Header_logo"
Source="/Images/rbkc_logo.png"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Visibility="Collapsed"
Margin="0,10,0,10"
Width="480"
Height="121" />
<Button x:Name="backButton" Margin="39,59,39,0"
Style="{StaticResource NavigationBackButtonNormalStyle}"
VerticalAlignment="Top"
AutomationProperties.Name="Back" />
</Grid>
请注意,无需使用正则表达式
答案 2 :(得分:1)
public boolean deleteS3Object(String multipartKey) {
if(StringUtils.isNotBlank(multipartKey)) {
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket("S3Bucket1").key(multipartKey).build();
DeleteObjectResponse deleteObjectResponse = s3Client.deleteObject(deleteObjectRequest);
if (deleteObjectResponse != null && deleteObjectResponse.sdkHttpResponse() != null) {
return deleteObjectResponse.sdkHttpResponse().isSuccessful();
}
}
return false;
}
用法:
public class RegularExpressionListAttribute : RegularExpressionAttribute
{
public RegularExpressionListAttribute(string pattern)
: base(pattern) { }
public override bool IsValid(object value)
{
if (value is not IEnumerable<string>)
return false;
foreach (var val in value as IEnumerable<string>)
{
if (!Regex.IsMatch(val, Pattern))
return false;
}
return true;
}
}