ASP.NET C#中的文本框验证 [英] text box validation in asp.net c#

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

问题描述

我有两个名为from date和date的文本框.
验证迄今必须大于一年.

I have two text box named as from date and to date .
validation give todate mustbe greater than one year.
how to give?

推荐答案

DateTime startdate = Convert.ToDateTime(txtDateFrom.Text);
DateTime endate = Convert.ToDateTime(txtDateTo.Text);
DateTime startdate = Convert.ToDateTime(txtDateFrom.Text);
DateTime endate = Convert.ToDateTime(txtDateTo.Text);
if (DateTime.Compare(startdate, enddate) < 0)
            {



}
其他
{



}
else
{

lblErrorMessage.Visible = true;
             lblErrorMessage.Text = "Start date should be less than end date ";



}



}


您可以使用自定义验证标签执行此操作.但是您也必须做一些JavaScript.通过使用以下命令,这对于检查文本框是否也具有值非常有用:

You can use the custom validation tags to do this. But you would have to do some javascript as well. This works well for checking if the text boxes have values as well, by using this:

<pre lang="vb"><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter date one"<br />
               ValidationGroup="validationGroup" Display="None" ControlToValidate="txtDateOne" /> </pre><br />



然后,对于自定义标签,应具有以下内容:



And then for the custom tag have something like this:

<asp:customvalidator id=""CustomValidator1"" runat=""server"" display=""None"" errormessage=""Please" make="" sure="" there="" is="" a="" one="" year="" mode="hold" xmlns:asp="#unknown" />                ValidationGroup="validationGroup" ClientValidationFunction="validateCorrespondence"></asp:CustomValidator>



在客户端验证字段中,您可以传递用于检查两个日期的javascript函数名称



In the client validation field you can pass the javascript function name that checks the two dates


尝试这种方式

try that way

function getTimeDiff(){
    var txtBoxStartDate = document.getElementById('<%=txtBoxStartDate.ClientID%>');
    var txtBoxEndDate = document.getElementById('<%=txtBoxEndDate.ClientID%>');

    var dtStart = new Date(txtBoxStartDate.value);
    var dtEnd = new Date(txtBoxEndDate.value);

    var diff = Math.floor((dtEnd.getTime() - dtStart.getTime())/(1000*60*60*24));
   alert(''The diff between dates is " + diff )

}


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

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