要触发自定义验证消息,可以按照以下步骤进行操作:
@if (!string.IsNullOrEmpty(errorMessage))
{
}
@code {
private string inputValue;
private string errorMessage;
private void ValidateInput()
{
if (!int.TryParse(inputValue, out _))
{
errorMessage = "Invalid input! Please enter a number.";
}
else
{
errorMessage = null;
}
}
}
oninput
事件中调用ValidateInput
方法来触发验证。
@if (!string.IsNullOrEmpty(errorMessage))
{
}
@code {
private string inputValue;
private string errorMessage;
private void ValidateInput()
{
if (!int.TryParse(inputValue, out _))
{
errorMessage = "Invalid input! Please enter a number.";
}
else
{
errorMessage = null;
}
}
}
现在,当用户输入非数字字符时,将显示自定义的验证消息。
请注意,上述示例中的error-message
类可以在CSS样式中定义为红色文本,以突出显示错误消息。