表单验证ASP.NET

时间:2014-05-15 08:30:46

标签: asp.net

我认为我忽略了这个问题,当我点击提交时没有任何内容出现,但当我在其中一个文本框中输入数据而不是全部时,我在脚本中收到错误消息。

我需要添加或更改哪些内容,以便在不在任何框中输入数据时收到消息

    protected void submit_Click(object sender, EventArgs e)
    {    
        if (jobsdate.Text == "" && firstjob.Text == "" && firstbudgetjob.Text == "" && secondjob.Text == "" && thirdjob.Text == "" && nightwork.Text == "" && otherwork.Text == "")
        {
            conform.Text = "Please Enter Data";
            return;
        }            
        else
        {
            execution(submittime.Text, jobsdate.Text, firstjob.Text, firstbudgetjob.Text, secondjob.Text, thirdjob.Text, nightwork.Text, otherwork.Text, totaljobs.Text, overbudget.Text);
            conform.Visible = true;
            submittime.Text = "";
            jobsdate.Text = "";
            firstjob.Text = "";
            firstbudgetjob.Text = "";
            secondjob.Text = "";
            thirdjob.Text = "";
            nightwork.Text = "";
            otherwork.Text = "";
            totaljobs.Text = "";
            overbudget.Text = "";                                    
        }           
    }

3 个答案:

答案 0 :(得分:0)

请考虑在ASP.net中使用Validator对象进行表单验证。 http://msdn.microsoft.com/en-us/library/vstudio/7kh55542(v=vs.100).aspx

答案 1 :(得分:0)

这一行

if (jobsdate.Text == "" && firstjob.Text == "" && 
    firstbudgetjob.Text == "" && secondjob.Text == "" && 
     thirdjob.Text == "" && nightwork.Text == "" && otherwork.Text == "")

要解析为true并输入if块需要将所有文本框都清空。 如果要在只有一个文本框为空时发出警告,则需要使用|| (OR运算符) 与string.IsNullOrWitheSpace

一起
if (string.IsNullOrWhiteSpace(jobsdate.Text) || 
    string.IsNullOrWhiteSpace(firstjob.Text) || 
    string.IsNullOrWhiteSpace(firstbudgetjob.Text) || 
    string.IsNullOrWhiteSpace(secondjob.Text) || 
    string.IsNullOrWhiteSpace(thirdjob.Text) || 
    string.IsNullOrWhiteSpace(nightwork.Text) || 
    string.IsNullOrWhiteSpace(otherwork.Text))
{
   conform.Text = "Please Enter Data";
    return;
}
else
{
    .....

答案 2 :(得分:0)

if (jobsdate.Text.Replace(" ","") == "" && firstjob.Text.Replace(" ","") == "" && firstbudgetjob.Text.Replace(" ","") == "" && secondjob.Text.Replace(" ","") == "" && thirdjob.Text.Replace(" ","") == "" && nightwork.Text.Replace(" ","") == "" && otherwork.Text.Replace(" ","") == "")

{
   conform.Text = "Please Enter Data";
   return;
}

不确定这是否适合您。

但如果用户将所有文本框都清空,这对我有用。

只需更换&&与||如果您希望在用户将单个文本框留空时显示消息