2011-11-21 160 views
0

我有(3)个文本框:txtPayRate,txtStartDate,txtFinishDate。在他们旁边,我有一个必需的字段验证程序:reqPayRate,reqStartDate,reqFinishDate。如果文本框留空或格式错误,我希望它返回我在属性菜单中指出的错误消息。我希望txtPayRate是一个数字,日期格式是mm/dd/yyyy。我不知道如何编写这段代码。如果我可以在txtPayRate和txtStartDate上获得帮助,我可以获得另一个。 C#必填字段验证器

回答

1

您可以在模型中定义它,MVC会产生客户方验证为您提供:

public class FooModel{ 

     [Required] 
     [DataType(DataType.Date)] 
     [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}"] 
     public DateTime RequestedStartDate{ 
      get; set; 
     } 

     [Required] 
     public decimal RequestedPayRate{ 
      get; set; 
     } 
}