文本框验证不工作 [英] Text box validation not working

查看:102
本文介绍了文本框验证不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在下面的测试代码的空白文本框。如果是空白的它返回的if语句下面指出的错误。这工作正常,但我也希望它检查是否有空格。我已经尝试了第一个以下内容:

 如果(String.IsNullOrWhiteSpace(txtFirstName.Text))

它不工作,虽然。我输入单词自行车在文本框中,但我拼写它像B IKE用空格来看看它是否会返回错误信息,并没有这样做。

 公共部分类frmPersonnel:System.Web.UI.Page 
{
保护无效btnSubmit_Click(对象发件人,EventArgs五)
$ { b $ b //lblError.Text =;

{
如果(txtFirstName.Text ==)
{
txtFirstName.BackColor = System.Drawing.Color.Yellow;
lblError.Text + =请输入名字< BR />中;
}

如果(txtLastName.Text ==)
{
txtLastName.BackColor = System.Drawing.Color.Yellow;
lblError.Text + =请输入姓氏<!BR />中;
}
如果(txtPayRate.Text ==)
{
txtPayRate.BackColor = System.Drawing.Color.Yellow;
lblError.Text + =请输入工资标准<!BR />中;
}
如果(txtStartDate.Text ==)
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text + =请输入开始日期<!BR />中;
}
如果(txtEndDate.Text ==)
{
txtEndDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text + =请输入结束日期<!BR />中;
}

的DateTime DT1;
日期时间DT2;

DT1 = DateTime.Parse(txtStartDate.Text);
DT2 = DateTime.Parse(txtEndDate.Text);


如果(DateTime.Compare(DT1,DT2)0)
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
txtEndDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text + =开始日期不得超过结束日期大于<!BR />中;
}

,否则
{
会话[txtFirstName] = txtFirstName.Text;
会话[txtLastName] = txtLastName.Text;
会话[txtPayRate] = txtPayRate.Text;
会话[txtStartDate] = txtStartDate.Text;
会话[txtEndDate] = txtEndDate.Text;
Server.Transfer的(frmPersonalVerified.aspx);
}
}
赶上(例外)
{

}
}
}


解决方案

我会改变以下类型的测试:

 如果(txtFirstName.Text ==)

要:

 如果(string.IsNullOrWhiteSpace(txtFirstName.Text))// .NET 4.0 + 

如果(string.IsNullOrEmpty(txtFirstName.Text))// .NET 4.0之前,

并为您的附加试验(字符串中不允许使用空格):

 如果(string.IsNullOrWhiteSpace(txtFirstName.Text)及&安培;!txtFirstName.Text.Contains())// .NET 4.0 + 

如果(string.IsNullOrEmpty(txtFirstName.Text)及和放大器;!txtFirstName.Text.Contains( ))// .NET 4.0之前,

请注意:



您将需要检查 lblError.Text 不含才能继续到下一个页面什么,因为这是握着你的错误。我只能看到的DateTime 测试,所以即使你的任何 TXT 控件都失败了验证,你还是转


Right now the code below tests for a blank text box. If it is blank it returns the error stated below the if statement. That works fine, but I also want it to check for white spaces. I have tried the following for the first one:

if (String.IsNullOrWhiteSpace(txtFirstName.Text))

It does not work though. I typed the word "Bike" into the text box, but I spelled it like "B ike" with a space to see if it would return the error message and it didn't.

public partial class frmPersonnel : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //lblError.Text = "";
        try
        {
            if (txtFirstName.Text == "")
            {
                txtFirstName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter first name<br />";
            } 

            if (txtLastName.Text == "")
            {
                txtLastName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter last name! <br />";
            }
            if (txtPayRate.Text == "")
            {
                txtPayRate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter pay rate! <br />";
            }
            if (txtStartDate.Text == "")
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter start date! <br />";
            }
            if (txtEndDate.Text == "")
            {
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter end date! <br />";
            }

            DateTime dt1;
            DateTime dt2;

            dt1 = DateTime.Parse(txtStartDate.Text);
            dt2 = DateTime.Parse(txtEndDate.Text);


            if (DateTime.Compare(dt1, dt2) > 0)
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Start Date must not be greater than End Date! <br />";
            }

            else
            {
                Session["txtFirstName"] = txtFirstName.Text;
                Session["txtLastName"] = txtLastName.Text;
                Session["txtPayRate"] = txtPayRate.Text;
                Session["txtStartDate"] = txtStartDate.Text;
                Session["txtEndDate"] = txtEndDate.Text;
                Server.Transfer("frmPersonalVerified.aspx");
            }
        }
        catch (Exception)
        {

        }
    }
}

解决方案

I would change the following type of test:

if (txtFirstName.Text == "")

To:

if (string.IsNullOrWhiteSpace(txtFirstName.Text)) // .NET 4.0+

if (string.IsNullOrEmpty(txtFirstName.Text)) // .NET before 4.0

And for your additional test (no spaces allowed in the string):

if (string.IsNullOrWhiteSpace(txtFirstName.Text) && !txtFirstName.Text.Contains(" ")) // .NET 4.0+

if (string.IsNullOrEmpty(txtFirstName.Text) && !txtFirstName.Text.Contains(" ")) // .NET before 4.0

Note:

You will need to check that lblError.Text doesn't contain anything in order to continue to the next page, as this is what holds your errors. I can only see the DateTime test, so even if any of your txt controls have failed the validation, you still transfer.

这篇关于文本框验证不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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