验证文本框并跳过窗体关闭时的验证 [英] Validating Textboxes and skipping Validations on form close

查看:72
本文介绍了验证文本框并跳过窗体关闭时的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我用来验证文本框的代码段。



C#代码



Following is the code snippet that I am using to validate my textboxes.

C# Code

private void TxtEmail_Validating(object sender, CancelEventArgs e)
{

    string StrEmail = TxtEmail.Text.Trim();
    if (StrEmail == "" || StrEmail.IndexOf("") > 0 || StrEmail.IndexOf('"') > 0 || StrEmail == " " || StrEmail.IndexOf(" ") > 0)
    {
        MessageBox.Show("Email Cannot be Left Blank");
        TxtEmail.Select();

    }

    Regex r = new Regex(@"@.");

    if (r.IsMatch(TxtEmail.Text))
    {
        AutoValidate = AutoValidate.Disable;

    }
    else
    {
        MessageBox.Show("Invalid Email Format");
        TxtEmail.Clear();
        TxtEmail.Focus();
    }

 }

private void TxtPassword_Validated(object sender, EventArgs e)
{

    string StrPass=TxtPassword.Text;
    if (StrPass == "" || StrPass.IndexOf("") > 0 || StrPass.IndexOf('"') > 0 || StrPass == " " || StrPass.IndexOf(" ") > 0)
    {
        MessageBox.Show("Invalid Password");
        TxtPassword.Focus();
    }

    Regex r = new Regex(@"@#");
    if (r.IsMatch(TxtPassword.Text))
    {

        AutoValidate = AutoValidate.Disable;

    }
    else
    {
        MessageBox.Show("Invalid Password");
        TxtPassword.Clear();
        TxtPassword.Focus();

    }


}







如果Regex中的参数匹配,我使用AutoValidate功能取消验证。但问题是当我使用AutoValidate或Set CauseValidation为false时,TextBox的验证是在第一次加载表单时完成的。但是当我删除texbox中的文本并重新输入它时,验证第二次不起作用并且不会抛出消息无效的电子邮件



In密码验证的情况。即使按照正则表达式中的参数输入密码后,它会抛出无效密码消息。我在此基于验证看到了问题,但没有一个问题能解决我的问题。



有谁能帮我纠正这个错误?我想在没有ErrorProvider的情况下实现验证,这是另一种选择。




I am using AutoValidate feature to cancel validation if the parameters within the Regex match.But the problem is when I use AutoValidate or Set CauseValidation to false,the validation of TextBox is done the first time the form is loaded. But when I remove the text within the texbox and re-enter it, validation doesn't work the 2nd time and does not throw the message "Invalid Email"

In the case of Password Validation.Even after entering password as per the parameters within the Regex,it throws "Invalid Password" message.I saw questions here based on validation but none of them have satisfactory solution to my question.

Can anyone help me to rectify this error? I want to achieve Validation without ErrorProvider which is another option.

推荐答案

让我们首先处理 TxtEmail_Validating ...



所有那些最终可能导致电子邮件不能留空的检查都是浪费时间 - 如果你选择一个合适的正则表达式进行测试。

但是如果你打算让它们进入,你需要在发现验证失败后适当退出。

此外,没有必要将焦点设置为您正在验证的控件,因为您尚未离开该控件。

最后,您遇到了所有麻烦修剪文本框中的文本,但在匹配正则表达式时没有使用它。



下一步 - 根据我的评论,没有必要关闭autovalidation当你在验证时 - 它有点失败的目的:-)让系统知道验证失败的正确方法是使用 CancelEventArgs [ ^ ]参数提供 - 即设置 e.Cancel = true;



我找到了一个合适的正则表达式这里 [ ^ ]以下内容将验证您的电子邮件文本框(注意我已将电子邮件文本设置为小写以允许更简单的正则表达式 - 在我看来总是一个好主意)...

Let's deal with TxtEmail_Validating first...

All those checks that could finally lead to the message "Email cannot be left blank" are a waste of time - if you choose an appropriate regex to test against.
But if you were going to leave them in, you need to exit appropriately once you have discovered that the validation has failed.
Also there is no need to set the focus to the control that you are validating because you have not yet left that control.
Finally, you went to all the trouble of trimming the text in the textbox, but didn't use it when matching to the regex.

Next - as per my comment, there is no need to turn autovalidation off while you are validating - it sort of defeats the purpose :-) The correct way to let the system know that the validation has failed is to use the CancelEventArgs[^] parameter provided - i.e. set e.Cancel=true;

I found an appropriate regex here[^] and the following will validate your email text box for you (note I've set the email text to lower case to allow for a simpler regex - always a good idea in my opinion) ...
private void TxtEmail_Validating(object sender, CancelEventArgs e)
{
    string StrEmail = TxtEmail.Text.Trim().ToLower();

    Regex r = new Regex(@"\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b");

    if (!r.IsMatch(StrEmail))
    {
        MessageBox.Show("Invalid Email Format");
        TxtEmail.Clear();
        e.Cancel = true;
    }
}



现在到你的 TxtPassword 验证:



根据我的评论 - 您使用了错误的事件 - 您应该使用 TxtPassword_Validat ing 而不是 Validat ed

有关TxtEmail验证的所有评论也适用于此处,我们的代码如下


Now to your TxtPassword validation:

As per my comment - you have used the wrong event - you should be using TxtPassword_Validating not Validated
All the comments regarding the TxtEmail validation also apply here, leaving us with code as follows

private void TxtPassword_Validating(object sender, CancelEventArgs e)
{
    Regex r = new Regex(@"^[a-zA-Z]\w{3,14}


);
if (!r.IsMatch(TxtPassword.Text))
{
MessageBox.Show( 无效密码);
TxtPassword.Clear();
e.Cancel = true ;
}
}
"); if (!r.IsMatch(TxtPassword.Text)) { MessageBox.Show("Invalid Password"); TxtPassword.Clear(); e.Cancel = true; } }



您仍然可以离开您尚未输入有效数据的位置,但仍想关闭表单 - 看看Sinisa Hajnal的解决方案,或者我的提示允许表单在存在无效数据时关闭 [ ^ ]


这是已解决的问题 [ ^ ]



如果这有帮助,请带是时候接受解决方案了。谢谢。
Here is resolved problem[^]

If this helps, please take time to accept the solution. Thank you.


这篇关于验证文本框并跳过窗体关闭时的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆