[英]How to dynamically correct a TextBox's Input Validation as soon as it loses focus?
I am confused at my if statement that says if the textbox is null, has whitespace, empty string, contains specific characters or has regex characters, it will validate as correct. 我对if语句(如果文本框为null,具有空格,空字符串,包含特定字符或具有正则表达式字符)感到困惑,它将被验证为正确。 But instead, it validates as incorrect when I input text before losing focus as required by if-statement to pass. 但是相反,当我输入文本之前,如果if语句要求传递焦点而失去焦点,它会被验证为不正确。 What am I doing wrong here? 我在这里做错了什么? I notice adding an if(!(..)) will make it work the opposite way but that isn't correct logic and I'm confused. 我注意到添加一个if(!(..))会使它以相反的方式工作,但这不是正确的逻辑,我感到困惑。
// firstNameTB Textbox to dynamically check validation
private void firstNameTB_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(firstNameTB.Text) || firstNameTB.Text == "" || firstNameTB.Text.Contains("") || !Regex.IsMatch(firstNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
{
firstNameTBL.Text = "First Name: *";
firstNameTBL.Foreground = new SolidColorBrush(Colors.Red);
firstNameTBL.FontWeight = FontWeights.Bold;
}
else
{
// set back to default layout
this.firstNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.firstNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.firstNameTBL.Text = "First Name:";
}
}
// lastNameTB Textbox to dynamically check validation
private void lastNameTB_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(lastNameTB.Text) || lastNameTB.Text == "" || lastNameTB.Text.Contains("") || !Regex.IsMatch(lastNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
{
lastNameTBL.Text = "Last Name: *";
lastNameTBL.Foreground = new SolidColorBrush(Colors.Red);
lastNameTBL.FontWeight = FontWeights.Bold;
}
else
{
// set back to default layout
this.lastNameTBL.ClearValue(TextBlock.ForegroundProperty);
this.lastNameTBL.ClearValue(TextBlock.FontWeightProperty);
this.lastNameTBL.Text = "Last Name:";
}
}
// emailAddressTB Textbox to dynamically check validation
private void emailAddressTB_LostFocus(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(emailAddressTB.Text) || emailAddressTB.Text == "" || !(emailAddressTB.Text.Contains("@") && emailAddressTB.Text.Contains(".")))
{
emailAddressTBL.Text = "Email Address: *";
emailAddressTBL.Foreground = new SolidColorBrush(Colors.Red);
emailAddressTBL.FontWeight = FontWeights.Bold;
}
else
{
// set back to default layout
this.emailAddressTBL.ClearValue(TextBlock.ForegroundProperty);
this.emailAddressTBL.ClearValue(TextBlock.FontWeightProperty);
this.emailAddressTBL.Text = "Email Address:";
}
}
The problem lies in the ckecks to see whether textboxes contain empty strings eg: 问题在于查看文本框是否包含空字符串,例如:
... || lastNameTB.Text.Contains("") || ...
... || firstNameTB.Text.Contains("") || ...
Because these always evaluate to true
- you get the behavior you described. 因为这些总是评估为true
您得到了描述的行为。
To fix this - simply remove these conditions from the if statements. 要解决此问题-只需从if语句中删除这些条件。
You can use String.Empty
to check the String is empty or not: 您可以使用String.Empty
来检查字符串是否为空:
if (string.IsNullOrWhiteSpace(lastNameTB.Text) ||
lastNameTB.Text == String.Empty ||
!Regex.IsMatch(lastNameTB.Text, @"^[A-Z]{1}[a-z]+$"))
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.