date validation

how we used data validation on asp.net? date can't be insert greater than the current date.


Use a CompareValidator. Most people use this for comparing two values entered into two textboxes, but you can also use it to compare one entered value with a set value as in your case.

  <asp:CompareValidator id="Compare1" 
       ControlToValidate="TextBox1"
       Type="Date"
       runat="server"/>

In the code behind set Compare1.ValueToCompare = new DateTime(...); and Compare1.Operator = ValidationCompareOperator.LessThanEqual;

Also, Remember: You should always validate on the Server as well as the client, because clientside validation is easy to switch off or by-passed. I would suggest you look at Fluent validation for this.


Make use of the CustomValidator will resolve your issues easily.

CustomValidator

or

You can use javascript to validate your date like as following

var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();

if (myDate>today)
  {
  alert("Today is before 14th January 2010");
  }
else
  {
  alert("Today is after 14th January 2010");
  }

In javascript solution,

Make sure to set hrs, min, secs, and milliseconds to 0, if you just want to compare dates (ie day,month and year). Function to acheive the above mentioned is as follows,

function f_tcalResetTime (d_date) {
    d_date.setHours(0);
    d_date.setMinutes(0);
    d_date.setSeconds(0);
    d_date.setMilliseconds(0);
    return d_date;
}

If you compare dates, javascript actually call date.valueOf function behind the scenes, which returns the number of millisecond passed since midnight January 1, 1970.

链接地址: http://www.djcxy.com/p/46686.html

上一篇: Canon EDSDK Liveview的示例C代码?

下一篇: 日期验证